0
所以我們剛開始在大學的計算機科學課上介紹java。我們必須修改代碼來顯示「如果里氏量級是(用戶輸入的值)」「(答案是由用戶輸入確定的」我對編程完全陌生,所以我在這方面遇到了麻煩。所以基本上它需要抓住用戶在對話框中輸入的數字,並在這兩段文本之間打印出來,我們使用BlueJ進行編碼,我們運行該程序並在終端打開答案獲取用戶輸入的值並將其顯示在其他預選文本中。 Java
這是需要編輯的代碼:
/**
* 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);
}
}
正是我在找的東西。先生非常感謝您! – Michael 2013-04-29 19:11:39