我在Ruby工作,我试图测试用户的输入(变量kid
)三次相同。
我想让我的方法speak
无休止地被调用,直到用户输入" BYE"当被问到三个单独的问题时,三次分开。
现在,如果用户输入" BYE"甚至只有一个问题,终端和用户之间的整个对话结束。
有没有办法让#34; BYE"进行程序测试?被说了三次,只有一次说了三次,谈话结束了吗?
kid = gets.chomp
unless kid == "BYE"
speak
end
我不知道是否有一个非常简单的解决方案或只是一个复杂的解决方案,但任何答案都有帮助。
答案 0 :(得分:0)
您需要跟踪用户输入的次数" BYE"。当该数字达到3时,退出:
case
代码在非" BYE"上重置为0回答。
在处理不同的用户输入时,我发现public class MyCustomView extends LinearLayout{
private TextView textView1;
private TextView textView2;
public MyCustomView(Context context){
super(context);
LayoutInflater layoutInflater = LayoutInflater.from(context);
layoutInflater.inflater(R.layout.my_custom_view, this);
}
public MyCustomView(Context context, AttributeSet attrs){
super(context, attrs);
}
public MyCustomView(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
}
@Override
protected void onFinishInflate(){
super.onFinishInflate();
textView1 = (TextView)findViewById(R.id.tv1);
textView2 = (TextView)findViewById(R.id.tv2);
}
// here's where I get the null pointer for textView1
public void setTitle(String title){
textView1.setText(title);
}
}
语句在这种情况下非常方便,特别是与正则表达式相结合。
答案 1 :(得分:-2)
我建议寻找关于“循环”和“控制流”的红宝石教程。
我认为Ruby Primer lesson at rubymonk.com可能有您需要的东西。
此示例也可能有所帮助:
times = 0 # keep track of iterations
while times < 10 # repeatedly, while this condition is true
puts "hello!" # output "hello!"
times += 1 # increase count of iterations
end