Using the Java language program
Demonstrate multiway if statement
1.1 Generate an integer random number from -5 to 5.(Hint: Use Math.random())
1.2 Write a multiway if statement that will display whether the number is positive, negative or zero.
1.3 Be sure to test your code with all three cases
//TestCode.java import java.util.Random; public class TestCode { public static void main(String[] args) { Random rand = new Random(); int n = rand.nextInt(11)-5; System.out.print(n+" is "); if(n<0){ System.out.println("negative"); } else if(n>0){ System.out.println("positive"); } else{ System.out.println("zero"); } } }
Get Answers For Free
Most questions answered within 1 hours.