public class Test1
{
public static void main(String[] args)
{
int x = 3;
if (x > 0)
{
System.out.println("x is greater than 0");
}
else
{
System.out.println("x is less than or equal 0");
}
}
}
The following code should check your guess against the answer and print that it is too low, correct, or too high. However, the code has errors. Fix the code so that it compiles and runs correctly.
Line 7 is missing the starting (. Line 8 is missing the closing ". Line 9 should be == rather than = to test for equality. Line 12 should be System.out.println.
public class Test1
{
public static void main(String[] args)
{
int guess = 7;
int answer = 9;
if (guess < answer)
{
System.out.println("Your guess is too low");
}
else if (guess == answer)
{
System.out.println("You are right!");
}
else
{
System.out.println("Your guess is too high");
}
}
}
The following code should print βYou can go outβ if you have done your homework and cleaned your room. However, the code has errors. Fix the code so that it compiles and runs correctly.
The following code should print if x is in the range of 0 to 10 (including 0 and 10). However, the code has errors. Fix the errors so that the code runs as intended.
public class Test1
{
public static void main(String[] args)
{
int x = 3;
if (x >= 0 && x <= 10)
System.out.println("x is between 0 and 10 inclusive");
else System.out.println("x is either less than 0 or greater than 10");
}
}
public class Test1
{
public static void main(String[] args)
{
int x = -3;
if (x < 0)
{
System.out.println("x is less than 0");
}
else if (x == 0)
{
System.out.println("x is equal to 0");
}
else
{
System.out.println("x is greater than 0");
}
}
}
Finish the code below so that it prints You can go out if you have a ride or if you can walk and otherwise prints You can't go
out. Use a logical or to create a complex conditional.
Add an if statement and use a logical or (||) to join the conditions and print the one message. Also add an else statement and print the other message.
public class Test1
{
public static void main(String[] args)
{
double temp = 103.5;
if (temp > 100)
{
System.out.println("You have a fever");
}
else
{
System.out.println("You don't have a fever");
}
}
}
Finish the code to print It is freezing if the temperature is below 30, It is cold if it is below 50, It is nice out if it is below 90, or It is hot using nested if else statements.
public class Test1
{
public static void main(String[] args)
{
int temp = 100;
if (temp < 30)
{
System.out.println("It is freezing");
}
else if (temp < 50)
{
System.out.println("It is cold");
}
else if (temp < 90)
{
System.out.println("It is nice out");
}
else
{
System.out.println("It is hot");
}
}
}
Finish the code below to print your grade based on your score. The score is an A if you scored 92 or higher, a B if you scored 82 to 91, a C if you scored 72 to 81, a D if you scored a 62 to 71, or an E.