Given a int variable named yesCount and another int variable named noCount and an int variable named response write the necessary code to read a value into into response and then carry out the following: if the value typed in is a 1 or a 2 then increment yesCount and print out “YES WAS RECORDED” if the value typed in is a 3 or an 4 then increment noCount and print out “NO WAS RECORDED” If the input is invalid just print the message “INVALID” and do nothing else. ASSUME the availability of a variable, stdin, that references a Scanner object associated with standard input.

1
Given a int variable named yesCount and another int variable named noCount and an int variable named response write the necessary code to read a value into into response and then carry out the following: if the value typed in is a 1 or a 2 then increment yesCount and print out “YES WAS RECORDED” if the value typed in is a 3 or an 4 then increment noCount and print out “NO WAS RECORDED” If the input is invalid just print the message “INVALID” and do nothing else. ASSUME the availability of a variable, stdin, that references a Scanner object associated with standard input.

LANGUAGE: JAVA

CHALLENGE:

Given a int variable named yesCount and another int variable named noCount and an int variable named response write the necessary code to read a value into into response and then carry out the following: if the value typed in is a 1 or a 2 then increment yesCount and print out “YES WAS RECORDED” if the value typed in is a 3 or an 4 then increment noCount and print out “NO WAS RECORDED” If the input is invalid just print the message “INVALID” and do nothing else. ASSUME the availability of a variable, stdin, that references a Scanner object associated with standard input.

SOLUTION:


response = stdin.nextInt();
if (response == 1 || response == 2){
   yesCount++;
   System.out.print("YES WAS RECORDED");
}else if (response == 3 || response == 4){
   noCount++;
   System.out.print("NO WAS RECORDED");
}else
   System.out.print("INVALID");

One comment

  • GIJOE says:

    response = stdin.nextInt();

    switch (response)
    {
    case 1:
    case 2:

    System.out.print(“YES WAS RECORDED”);
    yesCount++;
    break;

    case 3:
    case 4:

    System.out.print(“NO WAS RECORDED”);
    noCount++;
    break;

    default: System.out.print(“INVALID”); break;

Add comment