我正在尝试编写一个程序,接收输入和输出文件,其中包含硬币列表和这些硬币的数量。
public class LayarKuisUmumNo1 extends Activity {
Button bt;
TextView tv;
RadioGroup rg;
RadioButton rbu1, rbu2, rbu3, rbu4;
public static String question[] = { "Negara terluas keempat di dunia"};
String answer[] = { "Amerika"};
String opts[] = { "Rusia", "Australia", "Amerika", "Indonesia" };
int position = 0;
public static int correct;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layar_kuis_umum_no1);
bt = (Button) findViewById(R.id.btnextu1);
tv = (TextView) findViewById(R.id.pertanyaanu1);
rg = (RadioGroup) findViewById(R.id.radioGroup2);
rbu1 = (RadioButton) findViewById(R.id.rbu1a);
rbu2 = (RadioButton) findViewById(R.id.rbu1b);
rbu3 = (RadioButton) findViewById(R.id.rbu1c);
rbu4 = (RadioButton) findViewById(R.id.rbu1d);
tv.setText(question[position]);
rbu1.setText(opts[position]);
rbu2.setText(opts[position + 1]);
rbu3.setText(opts[position + 2]);
rbu4.setText(opts[position + 3]);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton selectedans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
String selectedanstext = selectedans.getText().toString();
if (selectedanstext == answer[position]) {
correct++;
}
position++;
if (position < question.length) {
tv.setText(question[position]);
rbu1.setText(opts[position * 4]);
rbu2.setText(opts[position * 4 + 1]);
rbu3.setText(opts[position * 4 + 2]);
rbu4.setText(opts[position * 4 + 3]);
} else {
Intent in = new Intent(getApplicationContext(), LayarNilaiUmum1.class);
startActivity(in);
}
}
});
}
}
我对此代码的问题是,我无法从文件中获取硬币数量并确定每种硬币的类型。这是我正在使用的测试器方法的示例。
import java.io.*;
import java.util.*;
public class CookieJar {
public static void cashingIn(File input, File output){
try{
Scanner in = new Scanner(input);
PrintWriter writ = new PrintWriter(output);
double sum = 0;
if(in.hasNext()){
String next = in.nextLine();
Scanner help = new Scanner(next);
while(in.hasNextLine()){
int y = Integer.parseInt(next.substring(0, 1));
if(next.contains("pennies")){
sum += y*0.01;
}
if(next.contains("dimes")){
sum += y*.1;
}
if(next.contains("quarters")){
sum += y*.25;
}
if(next.contains("nickles")){
sum += y*.05;
}
if(next.contains("penny")){
sum += .01;
}
if(next.contains("dime")){
sum += .1;
}
if(next.contains("nickle")){
sum += .05;
}
if(next.contains("quarter")){
sum += .25;
}
if(sum == 0){
String find = String.format("%s", "You have no money in the jar");
writ.println(find);
}
String fixer = String.format("$%sf", sum);
writ.println("You have " + fixer + " in the jar");
}
help.close();
}
else{
String find = String.format("%s" , "You have no money in the jar");
writ.println(find);
}
in.close();
writ.close();
}catch(IOException e){
}
}
}
如果我进入成千上万,我还必须使这个数字有逗号。如果你需要更多的澄清,我可能还不够具备帮助我所需要的东西。谢谢你的任何帮助。我对formmating很困惑。
答案 0 :(得分:1)
这是一种扩展评论,但是这样可以帮助您自己解决问题。
你有一个测试很好,但它和代码正在测试/做太多。处理字符串和计算硬币至少是一种责任,与文件交互绝对是另一项责任。通过一次测试,您的测试将变得复杂和缓慢。
应用Single Responsibility Principle可以更轻松,更快速地进行测试:
//The ONLY responsibility of this class is to keep count of coin strings it is given
public class CookieJar {
private double sum = 0; //consider internally counting pennies with an int
public void addString(string next){
int y = Integer.parseInt(next.substring(0, 1));
if(next.contains("pennies")){
sum += y*0.01;
}
//etc (but no code that reads/writes to console or files)
}
public string summarize(){
if(sum == 0){
return String.format("%s", "You have no money in the jar");
}
String fixer = String.format("$%sf", sum);
return "You have " + fixer + " in the jar";
}
}
测试将更容易编写和更快,您可以在单独的测试中单独测试每种硬币类型,然后在它们全部工作时一起测试:
@Test
public void test3() {
CookieJar cookieJar = new CookieJar();
cookieJar.addString("32 nickels");
assertEquals("You have $1.60 in the jar", cookieJar.summarize());
}
现在,from文件代码看起来更简单:
//The ONLY responsibility of this class is to allow reading and writing of coin files
public final class CookieJarIo {
private CookieJarIo(){} //static class
public static void cashingIn(File input, File output){
try{
Scanner in = new Scanner(input);
PrintWriter writ = new PrintWriter(output);
CookieJar cookieJar = new CookieJar(); //use our other class
if (in.hasNext()){
String next = in.nextLine();
Scanner help = new Scanner(next);
while(in.hasNextLine()){
cookieJar.addString(next);
writ.println(cookieJar.summarize());
}
help.close();
} else {
writ.println(cookieJar.summarize());
}
in.close();
writ.close();
} catch(IOException e){
}
}
}
答案 1 :(得分:0)
你应该充分考虑@weston关于分离责任的答案。
但是,在此代码中,如果您注意到您希望输入文件包含交替整数和字符串的流,则可以使其工作,例如
32 nickels
1
nickel 42
quarters 1 penny
1 quarter 23 pennies 16
dimes 1 dime 1 dime 1 dime 1 dime
您可以使用Scanner in
int amountInPennies = 0;
while (in.hasNextInt()) {
int quantity = in.nextInt();
if (!in.hasNext()) {
System.err.println("Didn't have a unit! Stopping.");
break;
}
String unit = in.next();
switch (unit) {
case "penny":
case "pennies":
amountInPennies += 1 * quantity;
break;
case "nickel":
case "nickels":
amountInPennies += 5 * quantity;
break;
// etc.
}
}
BigDecimal amountInDollars = BigDecimal.valueOf(amountInPennies, -2);
System.out.println("You have " + amount + " in the jar.");
关于如何将金额格式化为货币,您应该查看NumberFormat.getCurrencyInstance()