Write a Java application that generates two random numbers using built in method Math. Random (ranging 5 to 8). The program should use another built in method to calculate the power of x^y. the x value is the first random number generated, y value is the second number generated. The program should request for the user response to continue the program. If the user response ‘YES’ then the program should generates two more random numbers and calculate the power of x^y until the user response ‘N0’. Display all the random numbers generated and their power to a message box
import java.util.Scanner;public class test {
public test() {
int x,y;
boolean flag = true;
while(flag) {
x = (int)(Math.random()*(4)+5);
y = (int)(Math.random()*(4)+5);
System.out.print(x+"\t"+y+"\t x^y=");
System.out.println((int)Math.pow(x, y));
Scanner sc = new Scanner(System.in);
System.out.print("是否需要继续?(Y/N)");
if(sc.nextLine().equals("Y")) {
flag = true;
}
else flag = false;
}
System.out.println("==================================");
}
public static void main(String[] args) {
new test();
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// The result to a message box
StringBuilder result = new StringBuilder();
while (true) {
System.out.print("Continue?(YES/NO)");
String flag = in.nextLine();
if ("YES".equalsIgnoreCase(flag)) {
System.out.println("Generates two random numbers...");
int x = getRandomNumber(3, 5);
int y = getRandomNumber(3, 5);
result.append("x=");
result.append(x);
result.append(" y=");
result.append(y);
result.append(" result=");
result.append(Math.pow(x, y));
result.append("\n");
} else if ("NO".equalsIgnoreCase(flag)) {
break;
} else {
System.out.println("Please input the YES or NO");
}
}
// a message box
JOptionPane.showMessageDialog(null, result, "information",
JOptionPane.INFORMATION_MESSAGE);
// System.out.println("The Result is:\n" + result);
System.out.println("Program end..byebye..");
System.exit(0);
} private static int getRandomNumber(int elementsCount, int lowerBound) {
return (int) Math.round(Math.random() * elementsCount) + lowerBound;
}}
import java.awt.*;
import java.awt.event.*;public class Test extends JFrame implements ActionListener
{
JButton doAgain;
JButton giveUp;
public Test()
{
setWindow();
}
//set window
public void setWindow()
{
Container container = getContentPane();
container.setLayout( new FlowLayout() );
doAgain = new JButton( "do again" );
doAgain.addActionListener(this);
container.add( doAgain );
giveUp = new JButton( "give up" );
giveUp.addActionListener(this);
container.add( giveUp );
setSize( 275, 100 );
setVisible( true );
}
//generate random digitals and caculate
public String doRandomThing()
{
int x,y;
x = ( int )( Math.random()*(8-5) + 5 ) + 1;
y = ( int )( Math.random()*(8-5) + 5 ) + 1;
int result = ( ( int )Math.pow( x, y ) );
return "x = " + x + "\ny = " + y + "\n x^y = " + result;
}
//handle button event
public void actionPerformed( ActionEvent event )
{
if( event.getSource() == doAgain )
{
JOptionPane.showMessageDialog( Test.this, doRandomThing() );
}
else if( event.getSource() == giveUp )
{
System.exit( 0 );
}
}
public static void main( String[] args )
{
new Test();
}
}
import java.awt.*;
import java.awt.event.*;public class Test extends JFrame implements ActionListener
{
JButton doAgain;
JButton giveUp;
public Test()
{
setWindow();
}
//set window
public void setWindow()
{
Container container = getContentPane();
container.setLayout( new FlowLayout() );
doAgain = new JButton( "do again" );
doAgain.addActionListener(this);
container.add( doAgain );
giveUp = new JButton( "give up" );
giveUp.addActionListener(this);
container.add( giveUp );
setSize( 275, 100 );
setVisible( true );
}
//generate random digitals and caculate
public String doRandomThing()
{
int x,y;
x = ( int )( Math.random()*(8-5) + 5 ) + 1;
y = ( int )( Math.random()*(8-5) + 5 ) + 1;
int result = ( ( int )Math.pow( x, y ) );
return "x = " + x + "\ny = " + y + "\n x^y = " + result;
}
//handle button event
public void actionPerformed( ActionEvent event )
{
if( event.getSource() == doAgain )
{
JOptionPane.showMessageDialog( Test.this, doRandomThing() );
}
else if( event.getSource() == giveUp )
{
System.exit( 0 );
}
}
public static void main( String[] args )
{
new Test();
}
}