我已经编写了一个Java程序,我想将其编写成一个Android应用程序,
我在java程序中有一个名为TowersOfHanoi的类,其输出以这种方式完成:
System.out.println("移动磁盘" + n +"来自" +开始+"到" +结束);
我有一个mainActivity文件,我从那里调用TowersOfHanoi但是我不知道如何将它从system.ouut ...更改为variable.getText.TowersOfHanoi等并使其工作。
MainActivity code:
package com.example.towersofhanoiapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private TowersOfHanoi towersOfHanoi;
private Button submit;
static TextView display;
private EditText discs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
towersOfHanoi = new TowersOfHanoi();
submit = (Button) findViewById(R.id.btnSubmit);
display = (TextView)findViewById(R.id.display);
discs = (EditText)findViewById(R.id.userInput);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String discsString= discs.getText().toString();
if (!discsString.isEmpty()){
int discsInt =Integer.parseInt(discsString);
TowersOfHanoi.solve(discsInt,"Peg A", "Peg B", "Peg C");
}
}
});
}
}
TowerOfHanoi Method
package com.example.towersofhanoiapp;
public class TowersOfHanoi {
public static void solve(int n, String start, String auxiliary, String end) {
if (n == 1) {
System.out.println("Move disk "+ n + " from " + start + " to " + end);
} else {
solve(n - 1, start, end, auxiliary);
System.out.println("Move disk " + n + " from " + start + " to " + end);
solve(n - 1, auxiliary, start, end);
}
}
}
答案 0 :(得分:0)
在setText()
上使用discs
,并将您的方法solve
转换为返回String
,而不是执行println。
最简单的方法是在方法开头创建StringBuilder
,然后将System.out.println
替换为builder.append
。
在方法结束时,您必须添加return builder.toString()
以及此方法的结果,您应该在setText
中使用。
public static void solve(...) {
StringBuilder builder = new StringBuilder();
if (...) {
....
} else {
....
}
return builder.toString();
}
答案 1 :(得分:0)
package com.example.towersofhanoiapp;
public class TowersOfHanoi {
public static String solve(int n, String start, String auxiliary, String end) {
if (n == 1) {
return "Move disk "+ n + " from " + start + " to " + end);
} else {
solve(n - 1, start, end, auxiliary);
return ("Move disk " + n + " from " + start + " to " + end);
solve(n - 1, auxiliary, start, end);
}
}
并点击
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String discsString= discs.getText().toString();
if (!discsString.isEmpty()){
int discsInt =Integer.parseInt(discsString);
display.setText(TowersOfHanoi.solve(discsInt,"Peg A", "Peg B", "Peg C"));
}
}
});
答案 2 :(得分:0)
在这种情况下你可以做什么(并且只有当你想保留那些System.out调用时)才能使System.out打印到另一个PrintStream。你可以这样做:
public ByteArrayOutputStream testSystemOut(){
final StringWriter stringWriter = new StringWriter();
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final PrintStream printStream = new PrintStream(byteArrayOutputStream);
System.setOut(printStream);
return byteArrayOutputStream;
}
然后你可以这样做:
public static final String THIS_IS_THE_SENTENCE_YOU_ARE_EXPECTING = "This is the sentence you are expecting!";
@Test
public void testSystemOut() throws Exception {
final SystemOut systemOut = new SystemOut();
final ByteArrayOutputStream printer = systemOut.testSystemOut();
System.out.print(THIS_IS_THE_SENTENCE_YOU_ARE_EXPECTING);
assertThat(printer.toString(), equalTo(THIS_IS_THE_SENTENCE_YOU_ARE_EXPECTING));
}
如果您想继续使用System.out,此解决方案只是一种解决方法。请注意,如果执行此操作,则所有System.out调用将不再打印到控制台。它们现在只打印到输出流,除非您通过保留对旧PrintStream的引用并在需要时打开/关闭来进行某种管理。