Java执行while循环,多个条件未正确执行

时间:2016-10-19 20:58:00

标签: java loops do-while

我必须编写一个Magic 8球程序来解决用户输入错误,我必须使用循环来做到这一点。

using System;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;

namespace BrokenRichTextBox
{
    class CustomRichTextBox : RichTextBox
    {
        private bool _didAddLayoutUpdatedEvent = false;

        public CustomRichTextBox() : base()
        {
            UpdateAdorner();
            if (!_didAddLayoutUpdatedEvent)
            {
                _didAddLayoutUpdatedEvent = true;
                LayoutUpdated += updateAdorner;
            }
        }

        public void UpdateAdorner()
        {
            updateAdorner(null, null);
        }

        // Fixing missing caret bug code adjusted from: https://stackoverflow.com/questions/5180585/viewbox-makes-richtextbox-lose-its-caret
        private void updateAdorner(object sender, EventArgs e)
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                Selection.GetType().GetMethod("System.Windows.Documents.ITextSelection.UpdateCaretAndHighlight", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(
                    Selection, null);
                var caretElement = Selection.GetType().GetProperty("CaretElement", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Selection, null);
                if (caretElement == null)
                    return;
                var caretSubElement = caretElement.GetType().GetField("_caretElement", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(caretElement) as UIElement;
                if (caretSubElement == null) return;
                // Scale slightly differently if in italic just so it looks a little bit nicer
                bool isItalic = (bool)caretElement.GetType().GetField("_italic", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(caretElement);
                double scaleX = 1;
                if (!isItalic)
                    scaleX = (1 / ScaleX);
                else
                    scaleX = 0.685;// output;
                double scaleY = 1;
                var scaleTransform = new ScaleTransform(scaleX, scaleY);
                caretSubElement.RenderTransform = scaleTransform; // The line of trouble
            }), DispatcherPriority.ContextIdle);
        }

        public double ScaleX
        {
            get { return (double)GetValue(ScaleXProperty); }
            set { SetValue(ScaleXProperty, value); }
        }
        public static readonly DependencyProperty ScaleXProperty =
            DependencyProperty.Register("ScaleX", typeof(double), typeof(CustomRichTextBox), new UIPropertyMetadata(1.0));

        public double ScaleY
        {
            get { return (double)GetValue(ScaleYProperty); }
            set { SetValue(ScaleYProperty, value); }
        }
        public static readonly DependencyProperty ScaleYProperty =
            DependencyProperty.Register("ScaleY", typeof(double), typeof(CustomRichTextBox), new UIPropertyMetadata(1.0));

    }
}

当我运行代码并使其成为空字符串时,它会打印出来但不允许它仍然运行其余代码并且不会循环返回并询问"你的问题是什么?"问号也是如此;打印出来"添加问号"但不会像它应该的那样循环回来。如果我提出的问题超过60个字符,代码仍会执行,不会循环返回并继续询问用户"您的问题是什么?"直到代码少于60个字符。我试图找出我在这里做错了什么。

1 个答案:

答案 0 :(得分:1)

移动okay = true; 之前您的if语句否定它,

okay = true;
if (questionStr.length() == 0) {
    System.out.println("Not allowed.");
    okay = false;
} else if (!(questionStr.charAt(length - 1) == '?')) {
    System.out.println("Add question mark.");
    okay = false;
} else if (questionStr.length() > 60) {
    okay = false;
}

发布后,您在条件okay之前无条件地将true设置为while (!okay);,因此循环始终结束。

相关问题