int num = (int)(Math.random()*100)+1
Scanner sc = new Scanner(System.in)
int guessNum = -1
while (guessNum != num) {
System.out.println("请输入1-100之间整数")
guessNum = sc.nextInt()
if (guessNum == num) {
System.out.println("中啦")
} elseif (guessNum <num) {
System.out.println("小啦")
} else {
System.out.println("大了")
}
}
扩展资料:编写思路
1、成1-100之间随机数
(int)(Math.random()*100)+1
提示用户输入数字,
Scanner sc=new Scanner(System.in)
int guessNum = sc.nextInt()
需要将随机数和用户输入的数字进行比较。
猜一次:
Scanner sc = new Scanner(System.in)
int num = (int)(Math.random()*100)+1
System.out.println("请输入0-100之间整数")
int guessNum = sc.nextInt()
if (guessNum == num) {
System.out.println("中啦")
} elseif (guessNum <num) {
System.out.println("小啦")
} else {
System.out.println("大了")
}
二、使用while循环
publicstaticvoid main(String[] args) {
int num = (int)(Math.random()*100)+1
Scanner sc = new Scanner(System.in)
while (true) {
System.out.println("请输入1-100之间整数")
int guessNum = sc.nextInt()
if (guessNum == num) {
System.out.println("中啦")
} elseif (guessNum <num) {
System.out.println("小啦")
} else {
System.out.println("大了")
}
}
}
三、最后用while() 括号中的条件表达式,当用户猜测的数和系统生成的数字不相等时,就需要继续循环。
最终版本完整代码如下。无注视。希望对你有帮助。package com
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.beans.PropertyChangeEvent
import java.beans.PropertyChangeListener
import java.util.ArrayList
import java.util.List
import javax.swing.JButton
import javax.swing.JFormattedTextField
import javax.swing.JFrame
import javax.swing.JSpinner
import javax.swing.SpinnerListModel
import javax.swing.event.ChangeEvent
import javax.swing.event.ChangeListener
public class MySwing extends JFrame
{
public JSpinner js
public MySwing()
{
setSize(350, 400)
setLayout(null)
setResizable(false)
add(getJSpinner())
add(getJButton())
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE)
setVisible(true)
}
public JSpinner getJSpinner()
{
List<String>s = new ArrayList<String>()
for (int i = 2i <= 200i++)
{
s.add("" + i)
}
js = new JSpinner(new SpinnerListModel(s))
js.setBounds(new java.awt.Rectangle(10, 10, 60, 20))
JFormattedTextField jf=new JFormattedTextField()
jf.setText(js.getValue().toString())
jf.addPropertyChangeListener(new MyPropertyChangeListener())
js.setEditor(jf)
js.addChangeListener(new MyChangeListener())
return js
}
public JButton getJButton()
{
JButton jb = new JButton()
jb.setBounds(new java.awt.Rectangle(40, 40, 60, 20))
jb.addActionListener(new MyAl(js))
return jb
}
public static void main(String[] aa)
{
new MySwing()
}
}
class MyChangeListener implements ChangeListener
{
public void stateChanged(ChangeEvent e)
{
JSpinner spinner = (JSpinner) (e.getSource())
JFormattedTextField jf= (JFormattedTextField)spinner.getEditor()
jf.setValue(spinner.getValue())
}
}
class MyPropertyChangeListener implements PropertyChangeListener
{
public void propertyChange(PropertyChangeEvent e)
{
JFormattedTextField jf = (JFormattedTextField) e.getSource()
JSpinner spinner = (JSpinner) jf.getParent()
String name = e.getPropertyName()
if ("value".equals(name))
{
try
{
spinner.setValue(jf.getValue())
}
catch (IllegalArgumentException iae)
{
}
}
}
}
class MyAl implements ActionListener
{
JSpinner js
public MyAl(JSpinner _s)
{
js = _s
}
public void actionPerformed(ActionEvent e)
{
JFormattedTextField s = (JFormattedTextField) js.getEditor()
String ss = s.getText()
System.out.println("您输入的是 " + ss + " 您觉得你输入的对么?")
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)