为什么这个Java在代码创建无限循环时会这样做?

时间:2016-10-24 21:41:04

标签: java loops infinite javahelp

我正在试图找出为什么我的while循环让我有一个无限循环,我无法进步,即使从逻辑上讲,对我来说它似乎应该有效。下面是有问题的代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_my_custom_home"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.android.shustudenthelper.MyCustomHomeActivity"
    tools:showIn="@layout/app_bar_my_custom_home">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/image_shu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/shuhome_logo" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/image_shu"
            android:layout_gravity="center_horizontal"
            android:text="SHU STUDENT HELPER"
            android:textAllCaps="true"
            android:textAppearance="@android:style/TextAppearance.Medium"
            android:textColor="@color/gray"
            android:textSize="24sp"
            android:textStyle="italic|bold" />


    </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ListView
                android:id="@+id/listview_selected_courses"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/activity_vertical_margin"
                android:animateLayoutChanges="true"
                android:choiceMode="singleChoice"
                android:divider="#FFCC00"
                android:dividerHeight="4px"
                android:state_activated="true"
                tools:listheader="COURSES:" />

            <ListView
                android:id="@+id/list_view_games"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/activity_vertical_margin"
                android:animateLayoutChanges="true"
                android:choiceMode="singleChoice"
                android:divider="#FFCC00"
                android:dividerHeight="4px"
                android:state_activated="true"
                tools:listheader="GAMES:" />

            <ListView
                android:id="@+id/list_view_events"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/activity_vertical_margin"
                android:animateLayoutChanges="true"
                android:choiceMode="singleChoice"
                android:divider="#FFCC00"
                android:dividerHeight="4px"
                android:state_activated="true"
                tools:listheader="EVENTS:" />
        </LinearLayout>

</LinearLayout>

据我所知,所有这个循环都说是打印和扫描功能,而操作不等于报价中的任何内容。为什么这不起作用?

3 个答案:

答案 0 :(得分:4)

因为你的情况总是如此。替换||与&amp;&amp;。

或者你可以改写为:

while (!(oper == '+' || oper == '*' || oper == '/' || oper == '-' || oper == '%'))

在逻辑上等同于:

while (oper != '+' && oper != '*' && oper != '/' && oper != '-' && oper != '%')

换句话说:

NOT(A = X || A = Y)

与:

相同
A != X && B != Y

或者您使用KISS原则:

while (true) {
    System.out.print("Please enter an operation: ");
    oper = input.next().charAt(0);

    if ("+*/-%".indexOf(oper) != -1) {
        break;
    }
}

或者您使用decomposition

do {
    System.out.print("Please enter an operation: ");
    oper = input.next().charAt(0);
} while (!isValidOperation(oper));

...

public boolean isValidOperation(char oper) {
    return oper == '+' || oper == '*' || oper == '/' || oper == '-' || oper == '%';
}

答案 1 :(得分:1)

替换

while (oper != '+' || oper != '*' || oper != '/' || oper != '-' || oper != '%');

while (oper != '+' && oper != '*' && oper != '/' && oper != '-' && oper != '%');

目前,你的情况总是如此。

答案 2 :(得分:0)

要退出循环,操作员在

行输入
oper = input.next().charAt(0);

必须与'+', '-', '*', '/''%'不同。如果输入了这些字符中的任何一个,则循环将重复。显然,这就是你正在做的事情。

因此,一个明显的解决方案是用||替换while子句的&&操作数。

另一个解决方案是使用switch语句,它更具可读性(更快):

boolean loop = true;
do {
  System.out.print("Please enter an operation: ");
  oper = input.next().charAt(0);
  switch (oper) {
    case '+':
    case '-':
    case '*':
    case '/':
    case '%':
      loop = false;
      break;
} while (loop);