how would you restructure this code so it doesnt use continue and break? i have tried but have had no luck. thanks
import java.util.*;
public class q6 {
public static void main(String args[]) {
int Number;
Scanner sc = new Scanner(System.in);
while (true) // seemingly an infinite loop
{
System.out.print("Enter a positive integer ");
System.out.println("or 0 to exit ");
Number = sc.nextInt();
if (Number == 0)
break;
else if (Number < 0);
System.out.print("Squareroot of " + Number);
System.out.println(" = " + Math.sqrt(Number));
//continue lands here at end of current iteration
}
//break lands here
System.out.println("a zero was entered");
}
}
import java.util.*;
public class q6 {
public static void main(String args[]) {
int Number;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a positive integer ");
System.out.println("or 0 to exit ");
Number = sc.nextInt();
while (Number>0)// looping while number >0
{
System.out.print("Squareroot of " + Number);
System.out.println(" = " + Math.sqrt(Number));
Number = sc.nextInt();
}
System.out.println("a zero was entered");
}
import java.util.*;
class q6 {
public static void main(String args[]) {
int Number;
Scanner sc = new Scanner(System.in);
while (instruct() && (Number=sc.nextInt())!=0) // seemingly an infinite loop
{
if (Number < 0);
System.out.print("Squareroot of " + Number);
System.out.println(" = " + Math.sqrt(Number));
//continue lands here at end of current iteration
}
//break lands here
System.out.println("a zero was entered");
}
static boolean instruct()
{
System.out.print("Enter a positive integer ");
System.out.println("or 0 to exit ");
return true;
}
}
Related
I want to use scanner for user input for the number of threads to run. So far, this java code is able to run both the number of the Fibonacci sequences and factorial calculation based on user input. How do I do allow user input for the number of threads?:
import java.io.IOException;
import java.util.Scanner;
class Fibonacci extends Thread {
#Override
public void run() {
int n1=0, n2=0, n3=1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the amount of Fibonacci: ");
int number = sc.nextInt();
for(int i=1; i<=number; i++) {
n1 = n2;
n2 = n3;
n3 = n1 + n2;
System.out.println(n1 + " ");
}
}
}
class Factorial extends Thread {
#Override
public void run() {
try {
long fact = 1;
int i = 1, n;
Scanner input = new Scanner(System.in);
System.out.print("Enter a number for the calculation of Factorial: ");
n = input.nextInt();
input.close();
while(i<=n)
{
fact = fact * i;
i++;
}
System.out.println("Factorial of "+n+" is: "+fact);
} catch (NumberFormatException ex) {
}
}
}
public class Assignment02 {
public static void main(String args[]) throws IOException {
Scanner input = new Scanner(System.in);
System.out.println("1.Fibonacci");
System.out.println("2.Factorial");
System.out.println("Choose your Option:");
String line = input.nextLine();
int option = Integer.parseInt(line);
switch (option) {
case 1:
System.out.println("Fibonacci Sequence SELECTED");
try {
Fibonacci fib = new Fibonacci();
fib.start();
fib.join();
} catch (Exception e) {
}
break;
case 2:
System.out.println("Factorial SELECTED");
try {
Factorial fact = new Factorial();
fact.start();
fact.join();
} catch (Exception e) {
}
break;
default:
System.out.println("Choose a valid option");
break;
}
}
}
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc != 0)
{
System.out.println("first number: ");
int firstNum = sc.nextInt();
System.out.println("second number: ");
int secondNum = sc.nextInt();
System.out.println("The sum of your numbers: " + (firstNum + secondNum));
}
}
}
So my intended goal is to have a script that will allow me to add two integers (chosen by user input with a scanner) and once those two are added i can then start a new sum. I'd also like to break from my while loop when the user inputs 0.
I think my error is that i can't use the != operator on the Scanner type Could someone explain the flaw in my code? (I'm used to python which is probably why I'm making this mistake)
You need to declare the variable out of while scope and update it until condition is not met
Try this:
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int firstNum = 1;
int secondNum = 1;
while(firstNum !=0 && secondNum != 0)
{
System.out.println("first number: ");
firstNum = sc.nextInt();
System.out.println("second number: ");
secondNum = sc.nextInt();
System.out.println("The sum of your numbers: " + (firstNum + secondNum));
}
}
}
You should have some kind of an "infinite" loop like so:
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("first number: ");
int firstNum = sc.nextInt();
if (firstNum == 0) {
break;
}
System.out.println("second number: ");
int secondNum = sc.nextInt();
if (secondNum == 0) {
break;
}
System.out.println("The sum of your numbers: " + (firstNum + secondNum));
}
}
}
Hi just remove the while block since it has no sence to use it
Here the corrected code
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("first number: ");
int firstNum = sc.nextInt();
System.out.println("second number: ");
int secondNum = sc.nextInt();
System.out.println("The sum of your numbers: " + (firstNum + secondNum));
}
}
You cannot compare the Object sc with an integer value 0. You can do the below code.
public static void main(String[] args)
{
try (Scanner sc = new Scanner(System.in)) {
System.out.println("first number: ");
int firstNum = sc.nextInt();
while(firstNum != 0)
{
System.out.println("second number: ");
int secondNum = sc.nextInt();
System.out.println("The sum of your numbers: " + (firstNum + secondNum));
System.out.println("first number: ");
firstNum = sc.nextInt();
}
}
}
or
public static void main(String[] args)
{
try (Scanner sc = new Scanner(System.in)) {
while(true)
{
System.out.println("first number: ");
int firstNum = sc.nextInt();
if(firstNum == 0) {
break;
}
System.out.println("second number: ");
int secondNum = sc.nextInt();
System.out.println("The sum of your numbers: " + (firstNum + secondNum));
System.out.println("first number: ");
firstNum = sc.nextInt();
}
}
}
It's ugly af but it will let you understand the process
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean continueRunning = true;
while (continueRunning) {
System.out.println("first number: ");
int firstNum = sc.nextInt();
System.out.println("second number: ");
int secondNum = sc.nextInt();
System.out.println("The sum of your numbers: " + (firstNum + secondNum));
continueRunning = firstNum != 0 && secondNum != 0;
}
}
}
also ugly but i will sleep better tonight.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
requestNumbersAndSum(scanner);
}
private static void requestNumbersAndSum(Scanner scanner) {
int firstNum = requestANum(scanner, "first number: ");
int secondNum = requestANum(scanner, "second number: ");
System.out.println("The sum of your numbers: " + (firstNum + secondNum));
requestNumbersAndSum(scanner);
}
private static int requestANum(Scanner scanner, String messageToUser) {
System.out.println(messageToUser);
int requestedNumber = scanner.nextInt();
if(requestedNumber == 0){
System.exit(0);
}
return requestedNumber;
}
}
package hw.loops.co.il;
import java.util.Scanner;
public class LoopsTargilMedium3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num;
do {
System.out.println("Please enter a number:");
num = input.nextInt();
if (num%2==0) {
System.out.println("The number " + num + " is ZUGI");
}
else {
System.out.println("The number " + num + " is E-ZUGI");
num++;
} while (num!=-1);
System.out.println("loop stoped");
}
}
}
Receiving this error:
Exception in thread "main" java.lang.Error:
Unresolved compilation problem:
Syntax error, insert "while ( Expression ) ;" to complete DoStatement
you have misplaced a closing bracket before while:
..
} //<-- missing this
}while (num!=-1);
System.out.println("loop stoped");
...
public class LoopsTargilMedium3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num;
do {
System.out.println("Please enter a number:");
num = input.nextInt();
if (num%2==0) {
System.out.println("The number " + num + " is ZUGI");
}
else {
System.out.println("The number " + num + " is E-ZUGI");
num++;
}
System.out.println("loop stoped");
}while (num!=-1);
}
}
please check do while loop syntax
//--------------------------
do {
// statements
} while (expression);
---------------------//
import java.util.Scanner;
public class LoopsTargilMedium3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num;
do {
System.out.println("Please enter a number:");
num = input.nextInt();
if (num%2==0) {
System.out.println("The number " + num + " is ZUGI");
}
else {
System.out.println("The number " + num + " is E-ZUGI");
num++;
}
}
while (num!=-1);
System.out.println("loop stoped");
}
}
I am just playing around with java and wanted to make a simple program where the user; me has to guess/type in the correct number until it's correct. What can I do so the program can keep running, printing out "Make another guess" until the user/me puts in the correct number. Maybe a boolean? I'm not sure. This is what I have so far.
import java.util.Scanner;
public class iftothemax {
public static void main(String[] args) {
int myInt = 2;
// Create Scanner object
Scanner input = new Scanner(System.in);
//Output the prompt
System.out.println("Enter a number:");
//Wait for the user to enter a number
int value = input.nextInt();
if(value == myInt) {
System.out.println("You discover me!");
}
else {
//Tell them to keep guessing
System.out.println("Not yet! You entered:" + value + " Make another guess");
input.nextInt();
}
}
You might want to use a while loop to repeat some code:
while (value != myInt) {
System.out.println("Not yet! You entered: " + value + ". Make another guess");
value = input.nextInt();
}
System.out.println("You discovered me!");
This program would do the trick:
public static void main(String [] args){
int myInt = 2;
int value = 0;
Scanner input = new Scanner(System.in);
boolean guessCorrect = false;
while(!guessCorrect){
System.out.println("Not yet! You entered:" + value + " Make another guess");
value = input.nextInt();
if(value == myInt){
guessCorrect = true
}
}
System.out.println("You discover me!");
}
Simply introduce a loop.
import java.util.Scanner;
public class iftothemax {
public static void main(String[] args) {
int myInt = 2;
// Create Scanner object
Scanner input = new Scanner(System.in);
for(;;) {
//Output the prompt
System.out.println("Enter a number:");
//Wait for the user to enter a number
int value = input.nextInt();
if(value == myInt) {
System.out.println("You discover me!");
break;
}
else {
//Tell them to keep guessing
System.out.println("Not yet! You entered:" + value + " Make another guess");
}
}
}
}
I'm making a simple guess game but I don't know how to ask the player if the want to play the game again.Everytime the game ends I have to run the game again so I want to add this feature to it.I looked in some other posts but they didn't help.Here's the code:
import java.util.*;
public class guessNumber{
private static Scanner userInput = new Scanner(System.in);
public guessNumber(){
System.out.println("~~~Guess Game~~~");
}
public void guessGame(){
System.out.println("Enter the maximum number:");
int maxNum = userInput.nextInt();
System.out.println("Guess a number between 0 and " + maxNum + ":");
int randomNumber = (int) (Math.random() * maxNum);
boolean gameOn = true;
int numberOfTries = 0;
while(gameOn){
boolean printOthers = true;
numberOfTries++;
int number = userInput.nextInt();
if(number > maxNum){
System.out.println("Please enter a number between 0 and " + maxNum + ".");
printOthers = false;
}
if(printOthers){
if(number == randomNumber){
System.out.println("=================================");
System.out.println("You guessed the right number xD.");
System.out.println("Your tried " + numberOfTries + " times.");
System.out.println("=================================");
}else if(number > randomNumber){
System.out.println("Try a lower number");
}else if(number < randomNumber){
System.out.println("Try a higher number");
}
else{
System.out.println("Please enter a number between 0 and " + maxNum + ".");
}
}
}
}
public static void main(String[] args){
guessNumber guess = new guessNumber();
guess.guessGame();
}
}
In your main use a while loop. In the loop first call your guessGame() then, similarly to how you ask to enter a number, you ask if they want to play again (Y/N ?) and if they say no you break the loop otherwise you go ahead...
Add your calling method in main method with in a while loop
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
do{
//call your game
System.out.println("Do you want to play again. Press y");
}
while(scanner.next().equals("y"));