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");
}
}
Related
How to add a roll dice method and a check guess method?
My code:
package oui;
import java.util.Scanner;
public class dicecalc {
public static void main(String args[]) {
Scanner kb = new Scanner(System.in);
int numGuess;
System.out.println("Enter the number the numer the dice will roll: ");
numGuess = kb.nextInt();
int dice1=(int)(Math.random()*6+1);
int dice2=(int)(Math.random()*6+1);
int sum = dice1 + dice2;
System.out.println("Roll: total = " + sum);
{
if (sum != numGuess) {
System.out.println("Sorry with a " + sum + " You LOSE :(");
} else {
System.out.println("Woah!!! With a " + sum + " You WIN!!!!!!!");
}
}
}
}
I need to have this assignment resubmitted because I forgot those things but I don't know how to add it. Please help. I already have tried for days.
Here's one possible basic layout. Replace the ??? with some code!
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.print("Enter the number the numer the dice will roll: ");
int numGuess = kb.nextInt();
int sum = sumOfTwoDiceRolls();
System.out.println("Roll: total = " + sum);
{
if (!checkGuess(numGuess, sum)) {
System.out.println("Sorry with a " + sum + " You LOSE :(");
} else {
System.out.println("Woah!!! With a " + sum + " You WIN!!!!!!!");
}
}
}
public static int sumOfTwoDiceRolls() {
return ??? ;
}
public static int singleDiceRoll() {
// Math.random() * (max - min + 1) + min
return (int)(Math.random() * (6 - 1 + 1) + 1);
}
public static boolean checkGuess(int guess, int numberToGuess) {
return ??? ;
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("-------Welcome to the Radius calculator-------");
String input = new String (" ");
while(input.equals("END")==false) {
for (int i = 1; i < 5; i++)
for (int e = 5; e > 0; e--)
{
{
System.out.println("-----------------------------");
System.out.println("Hey you can use this calcultor " + e + " more time(s) till yoh have to purchase our full version only 3.99");
}
System.out.println("Please enter your Radius if not just type in END at any stage during the program");
Double num =sc.nextDouble();
System.out.println("Enter 1 if you would like to get the area");
System.out.println("Enter 2 if you would like to get the circumfrence");
int num1 = sc.nextInt();
if (num1 == 1) {
System.out.println("The area of the circle with the radius " + num + " is :" + (Math.PI) * (num * num));
} else if (num1 == 2) {
System.out.println("The circumfrence of the circle with the radius " + num + " is :" + (2) * (Math.PI) * (num));
} else {
System.out.println("No answer for you boss");
System.out.println("Try again!");
}
System.out.println("You have used this calculator effiently " + i + " time(s)");
System.out.println("-----------------------------");
}
}
}
}
Heres my code it works but when i type in END an ERROR comes up.(https://i.stack.imgur.com/ZZxEW.png)](https://i.stack.imgur.com/ZZxEW.png)
If theres any othere tips to make my code for effecient would be much appriciated too.
You forgot to ask for the end input during the loops.
The best way to ask for an input of different types is to use BufferedReader Class
BufferedReader reader =new BufferedReader(newInputStreamReader(System.in));
String input = null;
try {
input = reader.readLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
if (input.equals("END")) {
break;
}
After that you can use :
Integer.parseInt(input);
to cast from String to Integer
And don't forget to clean and simplify your code.
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;
}
}
So I've tried to make a Java calculator on console, and it's works decent, but when I get the result, I want it to restart like a loop, but I can't figure it out.
import java.util.Scanner;
public class calculator {
public static void main(String[] args0) {
Scanner test = new Scanner(System.in);
int x;
int y;
String c;
try {
System.out.println("Insert a number ");
x = test.nextInt();
System.out.println("insert a value e.g * / + -");
c = test.next();
System.out.println("Insert another number");
y = test.nextInt();
if (c.equals("*")) {
System.out.println("the total is " + x * y);
} else if (c.equals("+")) {
System.out.println("the total is " + (x + y));
} else if (c.equals("-")) {
System.out.println("the total is " + (x - y));
} else if (c.equals("/")) {
System.out.println("the total is " + (x / y));
} else {
System.out.println("you are an idiot");
}
} catch (Exception e) {
System.out.println("Please enter correct value.");
}
}
}
Use while + break. Below is the working version:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args0) {
Scanner test = new Scanner(System.in);
int x;
int y;
String c;
while (true) {
try {
System.out.println("Insert a number ");
x = test.nextInt();
System.out.println("insert a value e.g * / + -");
c = test.next();
System.out.println("Insert another number");
y = test.nextInt();
if (c.equals("*")) {
System.out.println("the total is " + x * y);
} else if (c.equals("+")) {
System.out.println("the total is " + (x + y));
} else if (c.equals("-")) {
System.out.println("the total is " + (x - y));
} else if (c.equals("/")) {
System.out.println("the total is " + (x / y));
} else {
System.out.println("you are an idiot");
}
} catch (Exception e) {
System.err.println("Please enter correct value.");
}
System.out.println("Do you wish to continue(y/n)?");
if (test.next().equals("n")) {
break;
}
}
}
}
You can use a while loop:
public static void main(String[] args0) {
Scanner test = new Scanner(System.in);
while(true) {
....
}
When you want to stop the calculator, you need enter ctrl - c.
Another way, use a boolean flag and let user dicide whether to continue:
public static void main(String[] args0) {
boolean flag = true;
Scanner test = new Scanner(System.in);
while(flag) {
....
System.out.println("enter y for continue, n for exit");
c = test.next();
if (c.equals("n")) {
flag = false;
}
}
}
Insert while loop before try and put everything below in it.put while(true) and this will repeat infinitely. If you want to press some key to stop make if condition and put break in it.
Scanner sc=new Scanner(System.in);
while(true){
...
if(sc.next()=='q') break; //this is if you want to quit after pressing q
...
}
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;
}
}