问答题

本题程序的功能是随机产生一个1~10的随机数,根据提示,用户可以猜这个随机数。例如程序随机产生的随机数是7,如果输入8,则提示“Try smaller…”并继续提示输入;如果输出6,则提示“Try bigger…”并继续提示输入;如果输入7,则会输出“Great!”并退出程序。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。
import java.io.*;
public class basic

public static void main(String[] args)

InputStreamReader it;
Bur feredReader in;
Ir = new InputStreamReader(System.in)
In = new BufferedReader(ir);
int ranNum = ______;
int guessNum = -1;
String str;
while (______)

System.out.print ("Please Input:");
try

______;
guessNum = Integer.parseInt(str);

catch (Exception e)


if(guessNum > ranNum)
System.out.println("Try smaller...");
else
System.out.println("Try bigger...");

System.out.println ("Great ! ");

【参考答案】

(int)(Math.random()*10)。
guessNum!=ranNum。
str=i......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

问答题
本题程序的功能是求两个交叉图形的并、减、交及异或。窗口中有4个单选按钮和一个图形面板,面板中有两个交叉的图形,选中其中一个单选按钮,图形面板中会以黑色填充的方式显示运算的结果。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.*; import javax.swing.*; public class advance public static void main(String[] args) JFrame frame = new AreaTestFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); class AreaTestFrame extends JFrame public AreaTestFrame() setTitle( advance ); setSize(WIDTH,HEIGHT); area1 = new Area(new Ellipse2D.Double(100,100,150,100)); area2 = new Area(new Rectangle2D.Double(150,150,150,100)); ______; panel = new JPanel() public void paintComponent(Graphics g) super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.draw(area1); g2.draw(area2); if (area ! = null) g2.fill(area); contentPane.add(panel,BorderLayout.CENTER); JPanel buttonPanel = new JPanel(); ButtonGroup group = new ButtonGroup(); JRadioButton addButtoh = new JRadioButton( 并 ,false); buttonPanel.add(addButton); group.add(addButton); addButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent event) area = new Area(); area.add(area1); area.add(area2); panel.repaint(); ); JRadioButton subtractButton = new JRadioButton( 减 ,false); buttonPanel.add(subtractButton); group.add(subtractButton); subtractButton.addActionListener(new ActienListener() public void actionPerformed(ActionEvent event) area = new Area(); area.add(areal); ______; panel.repaint(); ); JRadioButton intersectButton = new JRadioButton( 交 ,false); buttonPanel.add(intersectButton); group.add(intersectButton); intersectButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent event) area = new Area(); area.add(area1); area.intersect(area2); panel.repaint(); ); JRadioButten exclusiveOrButton = new JRadioButton( 异或 ,false); buttonPanel.add(exclusiveOrButton); group.add(exclusiveOrButton); exclusiveOrButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent event) area = new Area(); area.add(area1); ______; panel,repaint (); ); contentPane.add(buttonPanel,BorderLayout.NORTH); private JPanel pane1; private Area area; private Area area1; private Area area2; private static final int WIDTH = 400; private static final int HEIGHT = 400;
问答题
本题程序的功能是程序的主窗口有一个“登录”按钮和两个文本框——“请输入密码:”和“密码验证…”,用户在密码框中输入密码后单击“登录”按钮,然后与预先定义的密码比较(密码预设定为password),如果相同,则在“密码验证…,’文本框中提示“通过!”否则提示“失败!”。“密码验证…”文本框是只读的。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class simple public static void main(String[] args) LoginFrame frame = new LoginFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame,show (); class LoginFrame extends JFrame public LoginFrame() setTitle( simple ); setSize(WIDTH,HEIGHT); Container contentPane = getContentPane(); JPanel textPanel = new JPanel(); JLabel inputPassword = new JLabel( 请输入密码: ,JLabel.CENTER); MyPassword = new JPasswordField(20); textPanel.add(inputPassword); textPanel.add(myPassword); JLabel checkedPassword = new JLabel( 密码验证... ,JLabel.LEFT); CheckPassword = new JTextField(20); ______; textPanel.add(checkedPassword); textPanel.add(checkPassword); loginButton = new JButton( 登录 ); loginButton.addActionListener(new LoginAction()); textPane1.add(ioginButton); contentPane.add(textPanel); private class LoginAction implements ActionListener public void actionPerformed(ActionEvent event) char[] s = myPassword.getPassword(); String gets = new String(s); if(gets.equals(CHECKPASSWORD)) checkPassword,setText ( 通过! ); else checkPassword.setText ( 失败 ); public static final int WIDTH = 280; public static final int HEIGHT = 180; public static final String CHECKPASSWORD = password ; ______; private JTextField checkPassword; private JButton loginButton;