Need help with my JAVA homework ASAP thank you so much Write
Need help with my JAVA homework ASAP, thank you so much.
Write a program that uses a Scanner to read a number between 1 and 5 (N) and two different city names. If the number entered is odd it will print the first city N times; if the number is even it will print the second city N times. Test the program two times with two different inputs that show both conditions.Solution
import java.util.Scanner;
class ScannerTest{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int N=5;
System.out.println(\"Please type a number between 1 and 5:\");
int n=sc.nextInt();
String[] cities=new String[2];
System.out.println(\"Please type two cities:\");
for(int i = 0; i < cities.length; i++)
cities[i] = sc.next();
if(n%2==0)
for(int p=1;p<N;p++)
{
System.out.println(cities[1]);
}
else
for(int q=1;q<=N;q++)
{
System.out.println(cities[0]);
}
sc.close();
}
}
output:
D:\\anji>javac scanner.java
D:\\anji>java ScannerTest
Please type a number between 1 and 5:
5
Please type two cities:
vjwd gntr
vjwd
vjwd
vjwd
vjwd
vjwd
D:\\anji>java ScannerTest
Please type a number between 1 and 5:
2
Please type two cities:
vjwd gntr
gntr
gntr
gntr
gntr
D:\\anji>

