The code here I have works fine if I just want to ask the user to enter four digits:
here is the output of my code when I run the java application://java application that asks user to input binary numbers(1 or 0) and convert them to decimal numbers import java.util.Scanner; //program uses class scanner public class binarynumber{ //main method that executes the java application public static void main(String args[]){ //declares variables int digit; int base=2; int degree; double decimal; int binary_zero=0; int binary_one=1; //create scanner for object input Scanner input=new Scanner(System.in); System.out.println("Input a digit for a binary number(1 or 0):"); //prompt user to input a binary number digit=input.nextInt(); if(digit==1 || digit==0) System.out.printf("binary in discimal form is %f",digit*(Math.pow(base,0))); double ones_place=digit*Math.pow(base,0); System.out.println("Input a digit for a binary number(1 or 0):"); //prompt user to input a binary number digit=input.nextInt(); if(digit==1 || digit==0) System.out.printf("binary in decimal form is %f", digit*(Math.pow(base,1))); double tens_place=digit*Math.pow(base,1); System.out.println("Input a digit for a binary number(1 or 0):"); //prompt user to input a binary number digit=input.nextInt(); if(digit==1 || digit==0) System.out.printf("binary in decimal form is %f",digit*(Math.pow(base,2))); double hundreds_place=digit*Math.pow(base,2); System.out.println("Input a digit for a binary number(1 or 0):"); //prompt user to input a binary number digit=input.nextInt(); if(digit==1 || digit==0) System.out.printf("binary in decimal form is %f", digit*(Math.pow(base,3))); double thousands_place=digit*Math.pow(base,3); System.out.printf( "Output in decimal form is %f", ones_place+tens_place+hundreds_place+thousands_place); }//end main }//end class
The thing is, I want the java application to input more than four digits for the user and I want it to loop it manytimes f until the user ask it to stop. What should I do?Input a digit for a binary number(1 or 0):
1
binary in discimal form is 1.000000Input a digit for a binary number(1 or 0):
0
binary in decimal form is 0.000000Input a digit for a binary number(1 or 0):
0
binary in decimal form is 0.000000Input a digit for a binary number(1 or 0):
1
binary in decimal form is 8.000000Output in decimal form is 9.000000Press any ke
y to continue . . .