Trying to get the program to start over if the user enter ('y') after the program completes. [Java]
//here is the code. It complies fine and runs fine. I would like to add a while loop to start from the beginning is the user desires. import java.util.Scanner;
public class Convert {
Scanner scan; int num;
void getVal() {
int low, high,i,j;
System.out.println("Enter the low number:");
scan = new Scanner(System.in);
low = scan.nextInt();
System.out.println("Enter the high number:");
scan = new Scanner(System.in);
high = scan.nextInt();
System.out.println("Decimal\tBinary\tOctal\tHexadecimal");
for (i=low;i<=high;i++){
System.out.print(i);
num = i;
binary_convert();
oct_convert();
hex_convert();
}
}
void hex_convert() {
String hexa = Integer.toHexString(num);
int i=0;
do {
char letter = hexa.charAt(i);
System.out.print(letter);
i++;
} while (i < hexa.length());
System.out.println("");
}
void oct_convert() {
String oct = Integer.toOctalString(num);
for (int i = 0; i < oct.length(); i++) {
// Get letters with charAt method.
char letter = oct.charAt(i);
System.out.print(letter);
}
System.out.print("\t");
}
void binary_convert() { System.out.print("\t"); String bin = Integer.toBinaryString(num); int i=0; while (i < bin.length()) { // Get letters with charAt method. char letter = bin.charAt(i); System.out.print(letter); i++; } System.out.print("\t");
}
public static void main(String args[]) {
Convert obj = new Convert();
obj.getVal();
System.out.println("Would you like to try again with different numbers? y/n");
} }
Posted Jun 16, 2017, 7:47 PM UTC
12