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 8 years ago.
Improve this question
My if, else statement is giving me an error that is telling me to delete the word else. What should I do?
import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
double GradeAverage;
{
System.out.print("Welcome! Please enter in your mid-year grade average.");
}
Scanner kbdln = new Scanner(System.in);
{
GradeAverage = kbdln.nextDouble();
{
if (GradeAverage > 60 && GradeAverage <= 100);
{
System.out.print("You're passing your class!");
}
else if (GradeAverage > 0 && GradeAverage <= 60)
{System.out.print("Hook up with a smart classmate and STUDY!");}
}
}
}
}
There should be no semi-colon after the if statements.
The if statement should be as follows:
if(GradeAverage > 60 && GradeAverage <= 100)
First, I would get rid of all those { } you are using. It is way too much. Although it is correct to use them to declare a local namespace and absolutely necessary to use them with your if-else statements. But it is better not to use local namespaces if you are not absolutely sure what you are doing.
The second thing is you have a ; after your if statement. Which will be interpreted as an ; = empty statement. So your follow up else is completely alone – and that's why your IDE tells you to remove the else.
Here is a version of your code a little cleaned up :)
public class Test5 {
public static void main(String[] args) {
double GradeAverage;
System.out.print("Welcome! Please enter in your mid-year grade average.");
Scanner kbdln = new Scanner(System.in);
GradeAverage = kbdln.nextDouble();
if (GradeAverage > 60 && GradeAverage <= 100) {
System.out.print("You're passing your class!");
} else if (GradeAverage > 0 && GradeAverage <= 60) {
System.out.print("Hook up with a smart classmate and STUDY!");
}
}
}
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 5 years ago.
Improve this question
My code does not currently compile, I know that it is due to a lack of a return for my constructor- but if I could please get some help (as I have a version that I have already tried to fix), it would greatly be appreciated.
I have 2 files, one that has a constructor, and one that accesses that constructor. I should also note, that all of the help I found online said that I have to use an if else statement, but that leads to more errors.
My code for the main file:
import java.util.Scanner;//importing scanner
public class QuestionTwo {
public static void main(String[] args) {
int numberofDays;//these two lines define variables
int sharePoints;
Scanner keyboard = new Scanner(System.in);//activating scanner
System.out.print("Number of days in the period: ");//asking question
numberofDays = keyboard.nextInt();//obtaining input by defining a variable as a keyboard input
System.out.print("Share points on the first day: ");//asking another question
sharePoints = keyboard.nextInt();//obtaining input by defining a variable as a keyboard input
while (numberofDays < 10 || numberofDays > 20)
{
System.out.println("The number of days doesn’t meet the required criteria, enter it again");
System.out.print("Number of days in the period: ");
numberofDays = keyboard.nextInt();
}
SharePattern share = new SharePattern(numberofDays, sharePoints);
SharePattern.outPutTablePrinter(numberofDays, sharePoints);
//above two lines print day and share points, as well as the first line
of text (as it does not change)
}
}
Here is the code for the constructor (the one with the return error):
public class SharePattern {
private static int days;
private static int share;
public SharePattern(int numberofDays, int sharePoints)//constructor
{
numberofDays=days;
sharePoints=share;
}
public static int outPutTablePrinter(int numberofDays,int sharePoints){
for (int i = 2; i <= days; i++) {
if (days % 2 == 0)
if (i <= days / 2) {
share = share + 50;
} else {
share = share - 25;
} else {
if (i <= days / 2 + 1) {
share = share + 50;
} else {
share = share - 25;
}
}
}
System.out.println("The share points on the final day would be: "+share);
}
}
}
Any help would be appreciated, as I've been working on this problem all day and finally got finished when I came upon this problem.
First, you have an else {} else, which isn't valid
Not really clear which you're trying to accomplish..
Option 1
Remove the constructor and the static fields. You never use share variable.
Make your outPutTablePrinter method use the parameters you passed to it. And that method doesn't return anything. It's printing a value. Make it public static void
Option 2
If you want to keep the constructor, you need to flip the order of those equal statements and remove the static keyword.
Still make the method void
public class SharePattern {
private int days;
private int share;
public SharePattern(int numberofDays, int sharePoints){
days = numberofDays;
share = sharePoints;
}
public void outPutTablePrinter() {
And in the main method, you must use share.outPutTablePrinter()
It was actually extremely simple (sorry for my incompetence once again):
I should have put
return (sharePoints);
where i previously had nothing
This is what i suggest you if i understand correctly your code
SharePattern.class
private static int days;
private static int share;
public SharePattern(int numberofDays, int sharePoints)//constructor
{
days=numberofDays;
share=sharePoints;
}
public static void outPutTablePrinter(){
for (int i = 2; i <= days; i++) {
if (days % 2 == 0)
if (i <= days / 2) {
share = share + 50;
} else {
share = share - 25;
}
else
if (i <= days / 2 + 1) {
share = share + 50;
} else {
share = share - 25;
}
}
System.out.println("The share points on the final day would be: "+share);
}
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 7 years ago.
Improve this question
How i make this simple program to show me only one answer, if multiple conditions are satisfied like: if age<16 syso("you can't rent cars") and if age <18 syso("You can't vote").
For example, if i introduce 17 i want to display only ("You can't vote") not ("You can't vote" and "You can't rent cars").
I tried to use 2 conditions sticked by "&&", it didn't work.
Code from comments
import java.util.Scanner;
public class Assign1 {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
int age;
System.out.println("Insert age ");
age = x.nextInt();
if (age<16) {
System.out.println("You can't drive");
}
if (age<18 && age<16) {
System.out.println("You can't vote");
}
if (age<25) {
System.out.println("You can't rent cars");
}
if (age>25) {
System.out.println("You can do anything ");
}
}
}
This do what you want:
public class test {
public static void main(String[] args){
int age = 17;
if(age < 16){
System.out.println("You can't drive");
}
else if(age < 18){
System.out.println("You can't vote");
}
else if(age < 25){
System.out.println("You can't rent cars");
}
else{
System.out.println("You can do anything");
}
}
}
output:
You can't vote
The program print only the first if that is satisfied.
But i suggest you to study the conditional operators and logic. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
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 7 years ago.
Improve this question
I am trying to have the code print out the highest of the three grades; however, the if/else statement in the highest method is finding an error in return d. I have tried putting if(d>b && d>c) return d, and also else return d. But both times the program says it is unreachable. Can someone explain what I'm doing wrong? Thank you!
import java.util.Scanner;
public class Methods2 {
public static double average(double a){
double ave= a/3.0;
return ave;
}
public static double highest(double b, double c, double d){
if(b>c && b>d)
return b;
if(c>b && c>d);
return c;
return d;//unreachable code
}
public static void main(String[] args){
Scanner kb= new Scanner(System.in);
System.out.println("Enter your name.");
String name = kb.nextLine();
System.out.println("Enter your three grades.");
double b= kb.nextDouble();
double c= kb.nextDouble();
double d= kb.nextDouble();
double av= average(b+c+d);
System.out.println(av);
double high= highest(b,c,d);
System.out.println(high);
}
}
Yes. Because semicolon makes it an empty if body.
if(c>b && c>d);
return c;
should be
if(c>b && c>d)
return c;
or (the arguably better)
if(c>b && c>d) {
return c;
}
You could also use Math.max(double, double) to make this a one line method like
public static double highest(double b, double c, double d) {
return Math.max(d, Math.max(b, c));
}
because of the semicolon in the second if
if(c>b && c>d);
That terminates the conditional statement right there and return c; becomes a statement that will get executed regardless of condition, remove it
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 5 years ago.
Improve this question
Here is my code. I got an error as ".class expected".
What should I do to rectify it.
import java.util.*;
class Atm2
{
public static void main (String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the amount to be withdrawed");
int withdraw = in.nextInt();
System.out.println("Enter the amount in the account");
double acnt_balc = in.nextDouble();
if ((withdraw % 5 == 0) && (acnt_balc >= withdraw + 0.50))
double cur = acnt_balc - (withdraw + 0.50);
System.out.println(cur);
else
System.out.println(acnt_balc);
}
}
Try curly braces in the context of your if-else.
It should look like this:
if (isMoving) {
currentSpeed--;
} else {
System.err.println("The bicycle has already stopped!");
}
So you can see, that there is a if-block and a else-block. You have to say which code belongs to if and which belongs to else. You can do that with the braces.
Scanner requires you to import java.util.*;
I tried it works and also format properly , see below , it runs
import java.util.*;
class Atm2
{
public static void main (String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the amount to be withdrawed");
int withdraw = in.nextInt();
System.out.println("Enter the amount in the account");
double acnt_balc = in.nextDouble();
if ((withdraw % 5 == 0) && (acnt_balc >=(withdraw + 0.50)))
{
double cur=(acnt_balc-(withdraw + 0.50));
System.out.println(cur);
}
else
{
System.out.println(acnt_balc);
}
}
}
Also make sure your Filename is the same as class name , in this case Atm2.java
The Scanner Class in present in the 'java.util' package , since you forgot to import it and used Scanner sc=new Scanner(); the compliler is complaining, and remember to compile use javac filename.java , to run use java filename
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 8 years ago.
Improve this question
It gives me following error: The operator == is undefined for the argument type(s)
boolean, int
Syntax error on tokens, delete these tokens
package javaproject;
public class NestedIFandIFandElse {
public static void main(String[] args) {
int vanus = 50;
if (vanus == 40) {
System.out.println("first if ");
} else {
System.out.print("first else");
if (vanus == 50 ∣∣ vanus == 20) {
System.out.println("second if");
} else {
System.out.println("second else");
}
}
}
}
You should change ∣∣ to ||. They look the same, but they aren't:
if (vanus == 50 || vanus == 20)
I'm not certain how you entered that symbol, but ∣∣ is not ||;
if (vanus == 50 || vanus == 20 ) { // <-- The || or works here.
I changed it as above, and it compiles here.