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
解决方案 »
- 关于list添加多条数据的问题
- 子查询的问题select id,(select top 1 username from stu b where a.id=b.id) as usera from stu a group by id如何取得其中子查询的值呢
- 用java开发的知名软件有那些?
- 如何在菜单添加键盘快捷方式???
- 一个String中,如何知道,它里面的内容是中文还是英文
- 高手帮我
- 新手请教:我写applet时接收参数的语句出错,能否帮忙看一下是不是这样写?
- 从哪里可以下载jbuilder?
- 正则换行如何替换?
- myeclipse如何批量给包中的代码加上package xxxx
- 关于socket Connection reset的问题
- Java向服务器发送POST请求的问题
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();
}
}