Im very new to java And switch statements
Hello everyone,
Its my first time posting here and im very thankful for the work you guys do here
Heres a java assignment i just received from my class and i think i did some of it right.
Can someone please tell me why this wont compile properly.
Code :
import java.io.*;
class Question07{
public static void main(String args[]) throws IOException{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter GPA : ");
double gpa = Double.parseDouble(stdin.readLine());
if(gpa>=3.5){
int a = 1;
}else if(gpa>=3.49 & gpa >= 3.00){
int a = 2;
}else if(gpa>=2.99 & gpa>=2.5){
int a = 3;
}else if(gpa>=2.49 & gpa>=2.00){
int a = 4;
}else if(gpa<2.0){
int a = 5;
}else{
break;
}
int x = a;
switch(x){
case (1):
System.out.println("First Class\n");
break;
case(2):
System.out.println("Upper second class\n");
break;
case(3):
System.out.println("Lower secodn class\n");
break;
case(4):
System.out.println("Pass\n");
break;
case(5):
System.out.println("Fail\n");
break;
default:
System.out.println("Invalid GPA\n");
}
}
}
Its good if someone can tell me what iv done wrong and how to correct it with good visual example.I just dont seem to have a good time with java :(
Thanks in advance
Re: Im very new to java And switch statements
Quote:
why this wont compile
If you get compiler errors, please copy the full text of the error messages and paste it here.
One problem I see is the variable a needs to be defined outside of the if statements.
Re: Im very new to java And switch statements
This portion looks fudged up to me. You should revise it. First off your using a bit-wise operator to compare the expressions in your if statements. You should be using &&. Secondly, if gpa == 4.0, wouldn't all your else-if's expressions be true? You should format it so only one expression is true at a time.
Code java:
int a = 0; //Initialize outside of if-loop
if(gpa>=3.5){
a = 1;
}else if(gpa <= 3.49 && gpa >= 3.00){
a = 2;
}else if(gpa <=2.99 && gpa >= 2.5){
a = 3;
}else if(gpa <=2.49 && gpa >=2.00){
a = 4;
}else if(gpa < 2.0)
a = 5;
}else{
break;
}
switch(a){ //You don't need to assign it to another variable
}
Re: Im very new to java And switch statements
you declare the variable "a" globally or outside of the loop. Now you can run that code.
The problem is variable "a" is declared inside of the loop every time .
So the variable is not handle the values in out of the loop.
So it's got error in the line of this int x = a;
Re: Im very new to java And switch statements
Also revise where you are allowed to use the 'break' statement.
Re: Im very new to java And switch statements
Here is my take on it, hope it helps.
- Moved int a outside the loop.
- Removed the "break;" statement that was in the last else statement, and replaced it with a = -1;. I also added"(gpa<2.0 && gpa >= 0)" in the statement where a = 5 to enable to last else to get valid when you input a number below 0. This way, the last "default" switch will let you know you inputted an invalid number.
I must admit I am new to java as well, but I have found that trying to help other newbies helps me as well :)
Code :
import java.io.*;
class test{
public static void main(String args[]) throws IOException{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter GPA : ");
double gpa = Double.parseDouble(stdin.readLine());
int a;
if(gpa>=3.5){
a = 1;
}else if(gpa>=3.49 & gpa >= 3.00){
a = 2;
}else if(gpa>=2.99 & gpa>=2.5){
a = 3;
}else if(gpa>=2.49 & gpa>=2.00){
a = 4;
}else if(gpa<2.0 && gpa >= 0){
a = 5;
}else{
a = -1;
}
int x = a;
switch(x){
case (1):
System.out.println("First Class\n");
break;
case(2):
System.out.println("Upper second class\n");
break;
case(3):
System.out.println("Lower secodn class\n");
break;
case(4):
System.out.println("Pass\n");
break;
case(5):
System.out.println("Fail\n");
break;
default:
System.out.println("Invalid GPA\n");
}
}
}
Re: Im very new to java And switch statements
Try this code this is work correctly..I hope this will be help to you..
Code :
class Question07{
public static void main(String args[]) throws IOException{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter GPA : ");
double gpa = Double.parseDouble(stdin.readLine());
int a = 0 ;
if(gpa>=3.5){
a = 1;
}else if(gpa>=3.49 & gpa >= 3.00){
a = 2;
}else if(gpa>=2.99 & gpa>=2.5){
a = 3;
}else if(gpa>=2.49 & gpa>=2.00){
a = 4;
}else if(gpa<2.0){
a = 5;
}else{
}
int x = a;
switch(x){
case (1):
System.out.println("First Class\n");
break;
case(2):
System.out.println("Upper second class\n");
break;
case(3):
System.out.println("Lower secodn class\n");
break;
case(4):
System.out.println("Pass\n");
break;
case(5):
System.out.println("Fail\n");
break;
default:
System.out.println("Invalid GPA\n");
}
}
}
Re: Im very new to java And switch statements
Re: Im very new to java And switch statements
Firstly is a switch sequence necessary at all? --- Unless it's actually mentioned in the assignment? (can u post the wording please)
Also, have you checked the program logic by entering some gpa's? Enter 2.1 and tell us the result.
Re: Im very new to java And switch statements
use this code, you got error bcoz:-
1. you have to initialize the the variable
...
Quote:
Originally Posted by
RevChe
Hello everyone,
Its my first time posting here and im very thankful for the work you guys do here
Heres a java assignment i just received from my class and i think i did some of it right.
Can someone please tell me why this wont compile properly.
Code :
import java.io.*;
class Question07{
public static void main(String args[]) throws IOException{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter GPA : ");
double gpa = Double.parseDouble(stdin.readLine());
if(gpa>=3.5){
int a = 1;
}else if(gpa>=3.49 & gpa >= 3.00){
int a = 2;
}else if(gpa>=2.99 & gpa>=2.5){
int a = 3;
}else if(gpa>=2.49 & gpa>=2.00){
int a = 4;
}else if(gpa<2.0){
int a = 5;
}else{
break;
}
int x = a;
switch(x){
case (1):
System.out.println("First Class\n");
break;
case(2):
System.out.println("Upper second class\n");
break;
case(3):
System.out.println("Lower secodn class\n");
break;
case(4):
System.out.println("Pass\n");
break;
case(5):
System.out.println("Fail\n");
break;
default:
System.out.println("Invalid GPA\n");
}
}
}
Its good if someone can tell me what iv done wrong and how to correct it with good visual example.I just dont seem to have a good time with java :(
Thanks in advance