Use a text editor to create a comma-delmited file of user IDs and passwords. Rev
ID: 3686804 • Letter: U
Question
Use a text editor to create a comma-delmited file of user IDs and passwords. Revise the program below so that the user must first enter a correct ID and its associated passwords before playing.
package rockpaperscissors;
import java.util.Scanner;
public class RockPaperScissors
{
public static void main(String[] args)
{
int computer;
int person;
System.out.println("1 = Rock");
System.out.println("2 = Paper");
System.out.println("3 = Scissors");
computer = ((int)(Math.random() * 100) %
3 + 1);
Scanner input = new Scanner(System.in);
System.out.println("Please enter a number");
person = input.nextInt();
System.out.println("You chose" + person);
System.out.println("The computer chose" + computer);
if(computer == person)
System.out.println("The result is a tie");
else if(computer == 1 & person == 2)
System.out.println("You have won");
else if(computer == 2 & person == 3)
System.out.println("You have won");
else if(computer == 3 & person == 1)
System.out.println("You have won");
else if(computer == 2 & person == 1)
System.out.println("You have lost");
else if(computer == 3 & person == 2)
System.out.println("You have lost");
else if(computer == 1 & person == 3)
System.out.println("You have lost");
}
}
Explanation / Answer
Revise the program below so that the user must first enter a correct ID and its associated passwords before playing.
package rockpaperscissors;
import java.util.Scanner;
public class RockPaperScissors
{
public static void main(String[] args)
{
int computer;
int person;
System.out.println("1 = Rock");
System.out.println("2 = Paper");
System.out.println("3 = Scissors");
computer = ((int)(Math.random() * 100) %
3 + 1);
Scanner input = new Scanner(System.in);
System.out.println("Please enter a number");
person = input.nextInt();
System.out.println("You chose" + person);
System.out.println("The computer chose" + computer);
if(computer == person)
System.out.println("The result is a tie");
else if(computer == 1 & person == 2)
System.out.println("You have won");
else if(computer == 2 & person == 3)
System.out.println("You have won");
else if(computer == 3 & person == 1)
System.out.println("You have won");
else if(computer == 2 & person == 1)
System.out.println("You have lost");
else if(computer == 3 & person == 2)
System.out.println("You have lost");
else if(computer == 1 & person == 3)
System.out.println("You have lost");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
The java.lang.Math.random() returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range. When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression new java.util.Random
This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else. This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.