是或否问题和循环

时间:2015-10-19 04:22:08

标签: java list arraylist do-while

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ReminderYen {
    private String constant;

    public String getConstant() {
        return constant;
    }


    public void setConstant(String constant) {
        this.constant = constant;
    }

    public static void main(String[] args) {
       Scanner input = new Scanner (System.in);
       List<ReminderYen> reminderList = new ArrayList<ReminderYen>();
       char j;

       do
       {
           ReminderYen constant = new ReminderYen();
           System.out.println("Enter your reminder: ");
           constant.setConstant(input.next());

           reminderList.add(constant);

           System.out.println("Would you like to enter a new reminder (y/n)? ");
           String answer = input.next();
           j=answer.charAt(0);
           if(j=='y');
           System.out.println("Enter your reminder: ");
           constant.setConstant(input.next());

           System.out.println("Would you like to clear all your reminders (y/n)? ");
           if(j=='y');
           String ans = input.next();
           j=ans.charAt(0);
       }
       while(j== 'y');
       System.out.println("Your reminders are: ");
       for(ReminderYen c : reminderList){
           System.out.println(c . getConstant());
       }
    }
}

首先,我想显示&#34;输入您的提醒:&#34;在我输入提醒后,它会显示&#34;您想要输入新的提醒(是/否)?&#34;。如果&#34; y&#34;,它会再次显示&#34;输入您的提醒:&#34 ;;如果&#34; n&#34;,它会显示&#34;您要清除所有提醒(是/否)?&#34 ;;如果&#34; y&#34;它会再次显示&#34;输入你的提醒:&#34;它将清除所有提醒;如果&#34; n&#34;它会显示他/她输入的所有提醒。

2 个答案:

答案 0 :(得分:0)

这是空的 if(j=='y');

if

if(j=='y') { System.out.println("Enter your reminder: "); constant.setConstant(input.next()); } 语句下你想要做的只是在大致的刹车里面:

   System.out.println("Would you like to clear all your reminders (y/n)? ");
       if(j=='y');

您还试图在实际获得之前比较输入:

ReminderYen constant = new ReminderYen(); 
System.out.println("Enter your reminder: "); 
constant.setConstant(input.next()); 
reminderList.add(constant); 

while (j=='y') { 
    System.out.println("Would you like to enter a new reminder (y/n)? "); 
    String answer = input.next(); 
    j=answer.charAt(0); 
    if(j=='y') { 
        System.out.println("Enter your reminder: "); 
        constant.setConstant(input.next()); 
        reminderList.add(constant); 
    }
   else if (j == 'n') {
     System.out.println("Would you like to clear all your reminders (y/n)? "); 
     String ans = input.next(); 
     j=ans.charAt(0);
     if(j=='y') {
         // clear the reminders here
     }
}

System.out.println("Your reminders are: "); 
for(ReminderYen c : reminderList){ 
    System.out.println(c.getConstant()); 
}

此外,循环内的所有代码每次都会运行。如果你想在结束时只执行一次 - 你应该在循环后把它放在上。 我想你想要这样的东西:

{{1}}

答案 1 :(得分:0)

您必须首先了解Java中半结肠的重要性。在Java中,分号描述了语句的结尾。您在if语句和while语句末尾放置的分号表示条件/循环已结束而实际上没有执行任何操作。无论你想在if语句下做什么都应该在花括号下。

if(j=='y') {
       System.out.println("Enter your reminder: ");
       constant.setConstant(input.next());
}

同样适用于while循环

while(j== 'y') {
   System.out.println("Your reminders are: ");
   for(ReminderYen c : reminderList){
       System.out.println(c . getConstant());
   }
}

在开始编码之前,尝试通过Java,循环,条件语句的基础知识。它会对你有帮助。