I need help making it so that the entire script will loop from the beginning if the user types y at the end and the script ends if the user types n at the end. Thanks for any help.
package test123;
import java.util.Scanner;
public class test321 {
public static void main(String[] args) {
int n = 1;
int c;
String playAgain;
do {
System.out.println("Enter a number");
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
if ( n < 0 )
System.out.println("Number cant be a negative.");
else
{
int x=n*(n-1)*(n-2);
System.out.println("Factorial= "+x);
System.out.println("Do you want to continue? (y/n):");
playAgain = scanner.nextLine();
}while (!playAgain.equals("y"));
}
}
}
package test123;
import java.util.Scanner;
public class test321 {
public static void main(String[] args) {
int n = 1;
int c;
String playAgain;
do {
System.out.println("Enter a number");
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
if ( n < 0 )
System.out.println("Number cant be a negative.");
else
{
int x=n*(n-1)*(n-2);
System.out.println("Factorial= "+x);
System.out.println("Do you want to continue? (y/n):");
playAgain = scanner.nextLine();
} // end else
} // end do
while (playAgain.equals("y"));
}// end main
} // end class
Change
while (!playAgain.equals("y"));
to
while (!playAgain.equals("n"));
Related
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number");
int a = sc.nextInt();
boolean aValid = sc.hasNextInt();
System.out.println(aValid);
System.out.println("Enter Second number");
int b = sc.nextInt();
boolean bValid = sc.hasNextInt();
System.out.println(bValid);
if(aValid && bValid){
System.out.println("Sum of number is "+(a+b));
}
else{
System.out.println("Enter integer number");
}
}
}
I try to get input of a and b and validate that a and b are integers. But it takes three input. It gives the sum of the first two numbers and gives the validity of the last two.
you need to place check integer statements before taking input
boolean aValid = sc.hasNextInt();
int a = sc.nextInt();
boolean bValid = sc.hasNextInt();
int b = sc.nextInt();
Running this code makes the error fairly obvious. Some additional debug statements, running the code in debug or testing would also make it clear.
Scanner.nextInt() will throw an exception if it gets a non integer.
Scanner.hasNextInt() on the other hand returns a boolean value to warn you in advance whether it has an integer or not - it does not take the value from the Scanner.
This code has a couple of tiny corrections that will achieve the outcome you stated.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int a = 0;
int b = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number");
boolean aValid = sc.hasNextInt();
if (aValid) {
a = sc.nextInt();
} else {
sc.next(); //Discard the non-integer input.fr
}
System.out.println(aValid);
System.out.println("Enter Second number");
boolean bValid = sc.hasNextInt();
if (bValid) {
b = sc.nextInt();
}
System.out.println(bValid);
if (aValid && bValid) {
System.out.println("Sum of number is " + (a + b));
} else {
System.out.println("Enter integer numbers only");
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
int a = 0;
int b = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number");
boolean aValid = sc.hasNextInt();
if (aValid) {
a = sc.nextInt();
}
System.out.println(aValid);
System.out.println("Enter Second number");
boolean bValid = sc.hasNextInt();
if (bValid) {
b = sc.nextInt();
}
System.out.println(bValid);
if (aValid && bValid) {
System.out.println("Sum of number is " + (a + b));
} else {
System.out.println("Enter integer numbers only");
}
}
}
package task;
import java.util.*;
public class Task {
public static void main(String[] args) {
System.out.println("Enter \"t\" to terminate.");
for(;;){
Scanner input = new Scanner(System.in);
int i;
double I = Double.POSITIVE_INFINITY;
for(i = 0; i <= I; i++){
System.out.println("Integer " + i);
String a = input.next();
if(a.equals("c")){
break;
}
}
}
}
}
I am having trouble in prompting the user to enter "t" to end the for loop. I basically want the for loop to print out every single positive integer, and when I decide to end, I enter "t".
If I could get some help, I'd appreciate it. Thanks!
You could simply use a do while instead of a for+break
Do while :
System.out.println("Enter \"t\" to terminate.");
Scanner input = new Scanner(System.in);
String pressed;
int i = 0;
while (true) {
i=0;
do {
System.out.println("Integer " + i);
pressed = input.next();
i++;
} while (!pressed.equals("t"));
}
Note that you are not testing if the input is an integer.
hi guys I'm trying to make a lottery program And I'm trying to get the users input to after asking if they want to retry playing. But my program ends before reaching the while loop.
public class Main {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String yn = "";
Lottery.getTicket();
Lottery.generateWinningNumbers();
System.out.print("\nWould you like to try again? ");
while(input.hasNextLine())
{
yn = input.nextLine();
if(yn.equalsIgnoreCase("y"))
{
Lottery.getTicket();
Lottery.generateWinningNumbers();
}
else
{
System.out.println("Done");
}
}
input.close();
}
}
In my Lottery class:
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import java.util.Scanner;
public class Lottery {
public static Set<Integer> generateWinningNumbers()
{
Random rndNumbers = new Random();
TreeSet<Integer> winningNumbers = new TreeSet<Integer>();
int max = 40;
int min = 1;
int range;
int sixNum;
for(int i = 0; i < 6; i++)
{
range = max - min + 1;
sixNum = rndNumbers.nextInt(range) + min;
while(winningNumbers.contains(sixNum))
{
sixNum = rndNumbers.nextInt(range) + min;
}
winningNumbers.add(sixNum);
}
System.out.print("Winning Numbers: " + winningNumbers);
return winningNumbers;
}
public static Set<Integer> getTicket()
{
int userInput;
TreeSet<Integer> getNumbers = new TreeSet<Integer>();
Scanner input = new Scanner(System.in);
System.out.println("Enter your 6 numbers between 1-40: ");
for (int i = 0; i<6 ; i++)
{
System.out.print(i+1 + ": ");
userInput = input.nextInt();
while( userInput <1 || userInput > 40 || getNumbers.contains(userInput))
{
if (getNumbers.contains(userInput))
{
System.out.println("Number already picked");
userInput = input.nextInt();
}
if(userInput < 1 || userInput > 40)
{
System.out.println("Invalid. Pick a number between 1-40");
userInput = input.nextInt();
}
}
getNumbers.add(userInput);
}
input.close();
System.out.println("Your ticket was: " + getNumbers);
return getNumbers;
}
}//end of Lottery class
You are using the Scanner Object
Scanner input = new Scanner(System.in);
in Lottery.getTicket and you do
input.close();
This means that System.in will be closed for the rest of the program
Try passing the Scanner object from main to other classes and method that need it.
I want to display the statement inside the else whenever the user type a negative number, I know this while loop will be infinite loop but I don't know how to break it. I try to type in "break", but it shows error:
need a return statement.
Should I use only if-statement or am I writing the right code?
import java.util.Scanner;
class number{
public static void main(String[] args){
workers();
int numEmployee;
}
public static int workers(){
System.out.println("How many employees do you have?");
Scanner input = new Scanner(System.in);
int number = input.nextInt();
int numEmployee;
while(true){
if(number >= 0){
numEmployee = number;
return numEmployee;
}else{
System.out.println("Please enter a positive number");
}
}
}
}
Suggested changes:
import java.util.Scanner;
// Always capitalize your public class name (and the corresponding source file)
public class Number{
public static void main(String[] args){
// Get #/employees
int numEmployee = Number.workers();
System.out.println ("#/employees=" + numEmployee);
}
public static int workers(){
// Loop until we get a valid number
int number;
do {
System.out.println("How many employees do you have?");
Scanner input = new Scanner(System.in);
number = input.nextInt();
} while (number <= 0)
// Return the final value
return number;
}
}
First thing you want is to ask for input every loop. Also, you should have a return in your method for every condition. A compiler doesn't know your loop is infinite so it expects a return:
import java.util.Scanner;
class Number{
public static void main(String[] args){
int numEmployee = workers();
System.out.println("Number of empoyees: " + numEmployee);
}
public static int workers(){
int number = 0;
System.out.println("How many employees do you have?");
Scanner input = new Scanner(System.in);
while(true){
number = input.nextInt();
if(number >= 0){
return number;
}else{
System.out.println("Please enter a positive number");
}
}
return 0; //Added this
}
}
Your method should be:
public static int workers() {
System.out.println("How many employees do you have?");
Scanner input = new Scanner(System.in);
int number;
boolean isValid = false;
int numEmployee = 0;
while (!isValid) {
number = input.nextInt();
if (number >= 0) {
numEmployee = number;
isValid = true;
} else {
System.out.println("Please enter a positive number");
}
}
return numEmployee;
}
i need help in logic, i need the program to read an integer from user and then prints all the integers from 1 to num1. here's what i got :
import java.util.Scanner;
public class test
{
public static void main(String []args)
{
Scanner scan = new Scanner(System.in);
int num1;
int num2;
System.out.println("Enter any number:");
num1 = scan.nextInt();
while (num1<=num2) {
System.out.println(num+1);
}
}
}
Try this out:
import java.util.Scanner;
class test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number;
System.out.println("Enter any number:");
// Note : The below statement will fail if user does not enter integer value
number = scan.nextInt();
// You can use while loop as well but for loop provides cleaner approach for iteration
for (int i = 1; i <= number; i++) {
// Print numbers sequentially from 1 to number
System.out.println(i);
}
}
}
Program that reads num and prints from 1 to num
Try,
System.out.println("Enter any number:");
num1 = scan.nextInt();
int i=1;
while (i<=num1) {
System.out.println(i);
i++;
}
do like this
int num1=0;
int num2=0;
System.out.println("Enter any number:");
num1 = scan.nextInt();
while (num2 <= num1) {
System.out.println(num2);
num2++;
}
thanks alot guys! its been couple of years since last i coded a java program so im a little rusty! here's my final code :
import java.util.Scanner;
public class test
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
int num;
int a=1;
System.out.println("Enter any number:");
num=scan.nextInt();
while (a<=num)
{
System.out.println(a);
a++;}
}}