vendredi 27 octobre 2017

Writing a program that prints N sentences

So the code is supposed to print N number of sentences. So when the user enters 1 it prints one sentence, the first one in the array, "The light is red." Problem is when user enters 2 it also gives one sentence and when user enters 4 it prints 3 and so on...

This is the code I've written.

import java.util.Scanner;

public class RedYellowGreen {

    public static void main (String []args){
    System.out.println("How many sentences would you like to print?");
    Scanner scan= new Scanner (System.in);
    int length= scan.nextInt();
    String[]sent={"The light is Red.","The light is Yellow.","The light is Green."};
    for(int i=0;i<length;i++){
        if(length>0){
           System.out.println(sent[i]);

      }

        if(i==2){
          i-=3;
    }
     length--;

}

}
}   

Aucun commentaire:

Enregistrer un commentaire