i need help with a program in java

Trevor_5

i have to write a code that has a random number generated between 1-100 and the user has the choice to keep the number, discard the number and quit the program. and when the user quits the program displays the numbers kept and discarded, but i dont know how to use the character input for the user to chose. this is what i have Inline Code Example import java.util.*; public class Lab1A{ public static void main(String [] args){ int input=0; int K = 0; int D = 0; char Q = 0; for (int i=0; i<100; ){ double random = Math.random(); double x = random*100; int y = (int)x + 1; //Add 1 to change the range to 1 - 100 instead of 0 - 99 System.out.print("Random Number: "); System.out.print(y); System.out.println("\nEnter Option: (K, D, Q): "); char option = (char)input; if (option == K){ } if (option == D){ } if (option == Q){ System.exit(Q); } } } } Here

279 Views
About the Author
import java.util.*;
public class Lab1A{
	public static void main(String [] args){
		
		int input=0; 
		int K = 0; 
		int D = 0; 
		char Q = 0;
		
		
	
	for (int i=0; i<100; ){
	 double random = Math.random();

	    double x = random*100;

	    int y = (int)x + 1; //Add 1 to change the range to 1 - 100 instead of 0 - 99

	    System.out.print("Random Number: ");
	    System.out.print(y);
	    
	    System.out.println("\nEnter Option: (K, D, Q): ");
	    char option = (char)input;
JamesCherrill 4,394 Most Valuable Poster Moderator Featured Poster

Use a Scanner to read the users input. If you read a single char you can compare it in an if test, but you need quotes round the constant, eg

char in = ...
If (in == 'K') ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.