Cant find the issue here, help please [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Multiple markers at this line - Syntax error, insert ";" to complete Statement - The left-hand side of an assignment must be a variable - Syntax error, insert "AssignmentOperator Expression" to complete Assignment - Syntax error on token "else", invalid (
import java.util.Scanner;
public class NGG {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int x1, x2;
System.out.println("");
System.out.println("\n Guess X from 10 - 0: ");
x1 = scan.nextInt();
System.out.println("");
x2 = ((int)(Math.random()*10));
System.out.println(x2);
if (x2 == x1) {
System.out.println("Victory");
} else if(x2 > x1 && x2 < x1) {
System.out.println("Lose");
}
}
}

I made some changes to it.
nice game :)
public class NGG {
public static void main(String[] args) {
NGG ngg = new NGG();
ngg.game();
}
private void game(){
Scanner scan = new Scanner(System.in);
int enteredNumber, guessedNumber;
System.out.println("\n Guess number between 0 - 10: ");
enteredNumber = scan.nextInt();
guessedNumber = ((int)(Math.random()*10));
System.out.println(guessedNumber);
if (guessedNumber == enteredNumber) {
System.out.println("Victory");
} else {
System.out.println("Lost: number was " + guessedNumber);
}
}
}
did some minor refactoring and updated the logic to check if the guessed number/random number is same as the entered number then you get "victory" otherwise you Lose and shows you what the guessed/random number was.

Related

Trying to print out some sequence of numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to print out a sequence of numbers using this formula
Enter a number
if number is even divide number by 2.
But if number is odd multiply number by 3 and add 1
continue doing this until number becomes 1
sample input=3
Sample output=10 5 16 8 4 2
this is what I tried but still not getting it
package victor;
import java.util.Scanner;
public class proj {
public static void main(String[] args) {
Scanner put=new Scanner(System.in);
int temp=0;
boolean notOne=true;
System.out.println("input::: ");
int num=put.nextInt();
while(temp!=1){
if (num%2==0){
temp=num;
System.out.println(temp/2);
break ;
}
else {
temp=num;
System.out.println(temp*3+1);
break;
}
}
if(temp!=1){
notOne=false;
}
}
}
It's not working because you keep re-assigining the variable temp to the initially scanned num.
You keep checking if the initially scanned num is odd or even, when you should check if temp is odd or even.
You also break out of the loop for no reason.
And finally, you're not saving the result of the operations, you're only printing out the result.
Try to understand the points I mentioned above by noticing the differences between your code and the following:
while(temp!=1){
if (temp%2==0){
temp = temp/2;
}
else {
temp = temp*3+1;
}
System.out.println(temp);
}
You are not updating the value of temp. You are just printing it. Take the following statement
if (num%2==0){
temp=num;
System.out.println(temp/2);
break ;
}
Here you are setting temp to num and just printing temp/2 and never setting a value.
I wrote my version of it which is a bit more simpler. I hope this will help you. You can create a string to get a better output of course.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number:");
int number = scan.nextInt();
while (number != 1) {
number = number % 2 == 0 ? number / 2 : ((number * 3) + 1);
System.out.println("Number became " + number);
}
}
}
Try this:
public class Main
{
public static void main(String[] args) throws Exception
{
System.out.println("Starting...");
//Lets start the program, first we need
//the Scanner class to access to the input
Scanner stdin = new Scanner(System.in);
System.out.print("Type a num: ");
//I dont use: nextInt() because when asking for another input, will scan only
//the rest of the line (Maybe just \n - line break )
int num = Integer.parseInt(stdin.nextLine());
//Optional
int loops = 0;
while(num!=1){
//Pair, so num/2
if ( num %2 == 0){
num/=2;
}
else{
//num*3 +1
num=num*3 +1;
//Note that:
//1 + num*3
//Doesnt alter the result
}
System.out.println("num: "+num);
loops++;
}
System.out.println("total loops: "+loops);
}
}

While statement isn't excuting inside if statement? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am trying to make it print out a range of specified numbers. So if they select option 1 and put in 1 and 15, I want it to print out 1 to 15. Once it gets to the while statement though it just prints nothing.
import java.util.Scanner;
public class Lab4 {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.print("Please choose your choice from the following menu");
System.out.print("\n1) Print through all integer numbers between two given integers");
System.out.print("\n2) Display a right triangular pattern of stars");
System.out.println("\n3) Quit");
int userInput = in.nextInt();
if (userInput == 1) {
System.out.print("Enter the start number: ");
int firstInteger = in.nextInt();
System.out.print("Enter the second number: ");
int secondInteger = in.nextInt();
while (firstInteger < secondInteger);
System.out.print(firstInteger);
firstInteger++;
} else if (userInput == 2) {
System.out.print("Enter the height: ");
int triangleHeight = in.nextInt();
} else if (userInput == 3);{
System.exit(userInput);
}
in.close();
}
}
You should change :
while (firstInteger < secondInteger);
System.out.print(firstInteger);
firstInteger++;
to
while (firstInteger < secondInteger) {
System.out.print(firstInteger);
firstInteger++;
}
while (firstInteger < secondInteger);
This will be treated as two executable lines of instructions,
which is similar like
while (firstInteger < secondInteger)
;
Try to remove the ; after while statement

Can do-while loops only run with boolean variables? Java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am having some trouble with this small segment. Can do-while loops only run using boolean variables? I am trying to ask the "user" to enter "0" or "1" so that the program will either loop or end.
Error message:
Chapter4Practice.java:23: error: incompatible types: int cannot be
converted to boolean
} while (choice = 1);
^ 1 error
My code:
import java.util.Scanner;
public class Chapter4Practice
{
public static void main(String[] args)
{
int choice, num1, num2;
String input;
Scanner sc = new Scanner(System.in);
do {
System.out.print("Enter a number: ");
num1 = sc.nextInt();
System.out.print("Enter another number: ");
num2 = sc.nextInt();
System.out.println("The sum is " + (num1 + num2));
System.out.println("Do you want to do this again?");
System.out.println("(1 = yes, 0 = no)");
sc.nextLine();
choice = sc.nextInt();
} while (choice = 1);
} //End Main
} //End Class
Short answer: Yes, you can have simple or complex statements inside your do-while but at the end it will have to evaluate to either true or false
Also your statement should be == ( single = means assign, where == is evaluate)

My program shows an error, It says that a semicolon missing [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
import java.util.*;
public class Fact
program to find factorial numbers
{
Scanner sc=new Scanner(System.in);
int n;
Fact()
an empty constructor
{
}
void accept()
{
System.out.println("Enter the number");
n=sc.nextInt();
System.out.println(pact(n));
}
**int pact(int n)**
here is where my program says that it is missing a semicolon
(
if(n==1)
return 1;
else
return n*fact(n-1);
}
public static void main()
{
Fact obj=new Fact();
obj.accept();
}
}
Apart from the fact that
int pact(int n)
(
should be
int pact(int n)
{
You have String args[] missing as the arguments in the main method.
public static void main(String[] args){
After return statement you used closed curly, and no open curly is there .
You started with open parenthesis and closed with curly.
Just change that.
Try this. Removed invalid language constructs.
import java.util.Scanner;
public class Fact {
Scanner sc = new Scanner(System.in);
int n;
Fact() {
}
void accept() {
System.out.println("Enter the number");
n = sc.nextInt();
System.out.println(fact(n));
}
int fact(int n) {
if (n == 1)
return 1;
else
return n * fact(n - 1);
}
public static void main(String[] args) {
Fact obj = new Fact();
obj.accept();
}
}
If you copied the code correctly, you need to use a { instead of ( in this part of the code.
(
if(n==1)
return 1;
else
return n*fact(n-1);
}
so it should be
{
if(n==1)
return 1;
else
return n*fact(n-1);
}

Java cannot find symbol for scanner [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am trying to make this code work and it keep sending the error to the scaner.
(Test.java:7: error: cannot find symbol)
class Test{
public static void main(String[] args) {
int x;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number");
x = in.nextInt();
if (x<100)
x=x +5;
if (x<500)
x=x-2;
if (x>10)
x++;
else
x--;
System.out.println(x);
}
}
Correct your code to import java.util.Scanner; class like below and also change in.nextInt() to scanner.nextInt().
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int x;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number");
x = scanner.nextInt();
if (x < 100)
{
x = x + 5;
}
if (x < 500)
{
x = x - 2;
}
if (x > 10)
{
x++;
}
else
{
x--;
}
System.out.println(x);
}
}
1. You have used Scannerclass to take input while, you haven't told java that you are using it. For This you should import this import java.util.Scanner.
2. The class which has public static void main(String[] args) should be public.why
3. I think you should use if-else-if construct instead of many if because in your case if x = 20 it will be modified in all the three cases.
If you look at the Java doc here: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
You will see that Scanner needs:
import java.util.Scanner
and not
import java.io.*

Categories