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.*
Related
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.
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)
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 4 years ago.
Improve this question
This is my code for reversing the digits of an integer,
but the result gives each digit reversed but in a new line,
like if you give 341 to it , it gives you:
1
4
3
But I want to make it all come to the same line , is there a way to do it without changing the main code?
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter your number to reverse it ...");
int a=scan.nextInt();
while(a>0) {
i=a%10;
a/=10;
l++;
System.out.println(i);
}
System.out.println("Number of digits: "+l);
}
You could use print instead of println:
System.out.print(i);
Answer :
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter your number to reverse it ...");
int a=scan.nextInt();
while(a>0) {
i=a%10;
a/=10;
l++;
System.out.print(i);
}
System.out.println("Number of digits: "+l);
}
I think your code has few errors. It should be
int i = 0;
while(a>0) {
i=a%10;
a/=10;
System.out.print(i+" ");
i++;
}
In your code you are just picking digits and printing it in reversed order instead of really reversing it.
public static void main(String[] args)
{
int i,l=0,num=0;
Scanner scan=new Scanner(System.in);
System.out.println("Enter your number to reverse it ...");
int a=scan.nextInt();
while(a>0)
{
i=a%10;
a/=10;
num=(num*10)+i;
l++;
}
System.out.println("Number of digits: "+l);
System.out.println("Reversed Number: "+num);
}
Try this code instead.....
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);
}
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
Anybody know how to identify a given number is odd/even using addition or subtraction operator? I am new to coding and don't have an idea how to do this?
using subtraction.
int inputNumber = 12;
while(inputNumber>2)
{
inputNumber-=2;
}
if(inputNumber==1)
System.out.println("Odd Number");
else
System.out.println("Even Number");
using addition
int inputNumber = 12;
int absInputNumber = Math.abs(inputNumber)
int i = 0;
while(i < absInputNumber) {
i += 2
}
if(inputNumber==i)
System.out.println("Even Number");
else
System.out.println("Odd Number");
Java answer (without reference to Math class):
package odd.even.tester;
public class OddEvenTester {
public boolean isEven(int number) {
int evaluatedValue = number;
if (evaluatedValue < 0) {
evaluatedValue *= (-1);
}
while (evaluatedValue > 0) {
evaluatedValue -= 2;
}
return evaluatedValue == 0;
}
}
short test:
package odd.even.tester;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
public class OddEvenTesterTest {
#Test
public void testIsEven() {
OddEvenTester tester = new OddEvenTester();
assertTrue(tester.isEven(-10));
assertTrue(tester.isEven(-8));
assertTrue(tester.isEven(-6));
assertTrue(tester.isEven(-4));
assertTrue(tester.isEven(-2));
assertTrue(tester.isEven(0));
assertTrue(tester.isEven(2));
assertTrue(tester.isEven(4));
assertTrue(tester.isEven(6));
assertTrue(tester.isEven(8));
assertTrue(tester.isEven(10));
assertFalse(tester.isEven(-9));
assertFalse(tester.isEven(-7));
assertFalse(tester.isEven(-5));
assertFalse(tester.isEven(-3));
assertFalse(tester.isEven(-1));
assertFalse(tester.isEven(1));
assertFalse(tester.isEven(3));
assertFalse(tester.isEven(5));
assertFalse(tester.isEven(7));
assertFalse(tester.isEven(9));
}
}
There is another way to do it using "mod" which is the '%' symbol. If you have a number and use % 2, then if the answer is 0 it is even, if it is 1 it is odd.
Example:
int test = 12
if (test % 2 == 0)
System.out.print("Even.");
else
System.out.print("Odd.");
You can incorporate this in the addition and subtraction operators.