import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner keyboardInput = new Scanner(System.in);
String inputString;
char flag = 'y';
int number = 0;
int sum = 0;
while(flag = 'c' && flag = 'C') {
System.out.print("Enter number to be added");
number = keyboardInput.nextInt();
System.out.println("You have entered " + number);
sum = 0;
for(int i = 0; i < number; i++) {
sum = sum + i + 1;
}
System.out.println("The sum from 1 to " + number + " is " + sum);
System.out.print("Enter c or C to quit or any other key to continue:");
keyboardInput.nextLine();
inputString = keyboardInput.nextLine();
flag = inputString.charAt(0);
}
}
}
Here is the code i have it all figured out except i need to have the program to only continue on when the character c is entered instead of ending when c is entered
Try to use c
like a flag
not y
like this:
char flag = 'c';
And in your while loop use ==
:
while(flag == 'c' || flag == 'C') {
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.