i need help with a program in java

Trevor_5 0 Tallied Votes 312 Views Share

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

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,733 Most Valuable Poster Team Colleague 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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.