2013-04-22 48 views
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); 
} 
} 

回答

0

對於我來說,理解你所要求的有點難,但是如果我理解正確,就需要展示一些內容,例如「如果幅度爲3.8,很多人都覺得沒有破壞。」在這種情況下,你幾乎在那裏;你只需要使用字符串連接。這就像將兩個Strings一起沖洗成一個String。

quakeDamage已經擁有第二塊文本(在這種情況下,「感覺很多人,沒有破壞」)。您需要另一個字符串變量來保存最終結果。嘗試這樣的:

String result = "If the magnitude is " + magnitude + ", " + quakeDamage; 
System.out.println(result); 

這應該打印出整個文本。 (順便說一句,在您嘗試了一點之後,您可能需要編輯地震類中定義的文本,使其看起來更漂亮一些。注意大寫字母「Felt」將顯示在句子的中間)

+0

正是我在找的東西。先生非常感謝您! – Michael 2013-04-29 19:11:39

相關問題