Вы находитесь на странице: 1из 2

/*

* To change this template, choose Tools | Templates


* and open the template in the editor.
*/
package exponent;
//import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public static void main(String[] args) throws Exception {


BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int b,e;

System.out.println("Enter the base");


b = Integer.parseInt(br.readLine());
System.out.println("Enter the power");
e = Integer.parseInt(br.readLine());
int t = 1;
for(int i = 1;i <= e; i++);
{
t=t*b;
}
System.out.println(t);

}
// TODO code application logic here
}
for(int i=1;i<=e; i++ )
{
t=t*b;
}
A simple input test could be something along the lines of:

public boolean testInput(int e)


{
if(e>9||e<1)//where e is the inputted number
{
return false
}
else
{
return true;
}

}
boolean valid = false;
while(valid!=true)
{
e = Integer.parseInt(br.readLine());
if(testInput(e)==false)
{
System.out.println("Please enter a number between 1 and 9")
continue;
}
else
{
valid = true;
}
}
System.out.println("Enter the base");
b = Integer.parseInt(br.readLine());
System.out.println("Enter the power");
e = Integer.parseInt(br.readLine());

//with
do{
System.out.println("Enter the base");
b = Integer.parseInt(br.readLine());
}while(b > 9 || b < 1);

do{
System.out.println("Enter the power");
e = Integer.parseInt(br.readLine());
}while(e > 9 || e < 1);
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class StackOverflowAnswers{
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int b,e;

do{ //Asks for the base


System.out.println("Enter the base");
b = Integer.parseInt(br.readLine());
}while(b > 9 || b < 1); //If the base is not valid, it goes back to the
"do" statement, which asks for the base, again.

do{ //Asks for the power


System.out.println("Enter the power");
e = Integer.parseInt(br.readLine());
}while(e > 9 || e < 1); //If the power is not valid, it goes back to the
"do" statement, which asks for the power, again.

int t = 1;

for(int i = 1;i <= e; i++){ //No semicolon here


t=t*b;
}
System.out.println(b + " to the " + e + "th power is " + t); //Just added
some words and the base and the power for easier debugging and understanding.
}
}

Вам также может понравиться