Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取用户输入的值并将其显示在其他预选文本中。JAVA_Java_Printing_Io_Output_Bluej - Fatal编程技术网

获取用户输入的值并将其显示在其他预选文本中。JAVA

获取用户输入的值并将其显示在其他预选文本中。JAVA,java,printing,io,output,bluej,Java,Printing,Io,Output,Bluej,所以我们刚在大学计算机科学入门课上开始学习java。我们必须修改代码以显示“如果里氏震级为(用户输入值)”(答案由用户输入确定)我对编程一无所知,所以我在这方面遇到了麻烦。所以基本上,它需要抓取用户在对话框中输入的数字,并在这两段文本之间打印出来。我们使用BlueJ编码。我们运行程序,然后在终端中打开答案 这是需要编辑的代码: /** * Write a description of class Earthquake here. * * A class that describes the ef

所以我们刚在大学计算机科学入门课上开始学习java。我们必须修改代码以显示“如果里氏震级为(用户输入值)”(答案由用户输入确定)我对编程一无所知,所以我在这方面遇到了麻烦。所以基本上,它需要抓取用户在对话框中输入的数字,并在这两段文本之间打印出来。我们使用BlueJ编码。我们运行程序,然后在终端中打开答案

这是需要编辑的代码:

/**
* Write a description of class Earthquake here.
*
* A class that describes the effects of an earthquake.
* @author Michael Gerhart
* @version Version 1.0, 4/22/2013
*/
public class Earthquake
{
// instance variables
private double richter;
/**
* Constructor for objects of class Earthquake
* @param magnitude the magnitude on the Richter scale
*/
public Earthquake(double magnitude)
{
// initialise instance variable richter
richter = magnitude;
}
/**
* Gets a description of the effect of the earthquake.
*
* @return the description of the effect
*/
enter code here`public String getDescription(double magnitude)
{
String r;
if (richter >= 8.0)
r = "Most structures fall";
else if (richter >= 7.0)
r = "Many buildings destroyed";
else if (richter >= 6.0)
r = "Many buildings considerably damaged; "
+ "some collapse";
else if (richter >= 4.5)
r = "Damage to poorly constructed buildings";
else if (richter >= 3.5)
r = "Felt by many people, no destruction";
else if (richter >= 0)
r = "Generally not felt by people";
else
r = "Negative numbers are not valid";
return r;
}
}
这是运行程序的代码:

import javax.swing.JOptionPane;
/**
* Write a description of class EarthquakeTest here.
*
* A class to test the Earthquake class.
*
* @author Michael Gerhart
* @version 4/22/2013
*/
public class EarthquakeTest
{
public static void main(String[] args)
{
String input = JOptionPane.showInputDialog("Enter a magnitude on the Richter scale:");
double magnitude = Double.parseDouble(input);
Earthquake quake = new Earthquake(magnitude);
String quakeDamage = quake.getDescription(magnitude);
System.out.println(quakeDamage);
System.exit(0);
}
}

对我来说,理解你的要求有点困难,但如果我理解正确,你需要显示这样的内容,“如果震级是3.8,很多人都能感觉到,没有破坏。”在这种情况下,你就快到了;你只需要使用字符串连接。就像将两个字符串组合在一起形成一个字符串

quakeDamage已经保存了第二个文本块(在本例中为“许多人感觉到,没有破坏”)。您需要另一个字符串变量来保存最终结果。请尝试以下操作:

String result = "If the magnitude is " + magnitude + ", " + quakeDamage;
System.out.println(result);
这样就可以打印出整个文本


顺便说一下,在你做了一点实验之后,你可能想编辑地震课中定义的文本,使它看起来更漂亮一些。注意“感觉”中的大写字母会出现在句子的中间。