负数或小数

时间:2015-02-18 09:17:47

标签: java

大家好我只是想知道我的代码有什么问题,但我想我得到了一些逻辑错误  这将是该计划的输出

"Enter a float values: -4.6

 Do you want the (s)quare or square (r)oot of -4.6: r

 You must have a positive for square root.

 Do you want the (s)quare or square (r)oot of -4.6: s

 The number -4.6 squared has the value 21.159999122619638

 Do you want to enter more data ? y/n: n .

// ============================================= ===============

这是我得到的逻辑错误。

Enter a float values: -4

Do you want the (s)quare or square (r)oot of -4.0: s

You must have a positive for square root.

Do you want the (s)quare or square (r)oot of -4.0: s

The number -4.0 squared has the value 16.0

Do you want to enter more data ? y/n:

假设我将获得价值的平方而不是得到“你必须有一个正方形”的声明,这不会发生真正的必要,因为我没有选择R到“平方根-4: 希望你理解并帮助我.. 这是我得到的逻辑错误。

像这样,这应该是正确的答案。

Enter a float values: -4

Do you want the (s)squared or square (r)root of -4.0: s

The number -4.0 squared has the value 16.0

Do you want to enter more data ? y/n:
谢谢你们。我会等你的答案和帮助

BTW这是代码^^

{
    Scanner console = new Scanner (System.in);

    float FloatValue;
    char again = 'y';
    char letter;

    String token;
    String SqrSqrt; 

    do 

    {

        System.out.print("Enter a float values: ");
        FloatValue = console.nextFloat();

        System.out.print("Do you want the (s)quare or square (r)oot of " + FloatValue +": ");
        SqrSqrt = console.next();

        if (FloatValue < 0)
        {
        System.out.println("You must have a positive for square root.");


        System.out.print("Do you want the (s)quare or square (r)oot of " + FloatValue +": ");
        SqrSqrt = console.next();

        }


    switch(SqrSqrt)

        {

        case "r":   

        {

            System.out.println("The square root of " + FloatValue +" is " +  Math.sqrt(FloatValue) );       
            break;
        }   

        case "s":

            System.out.println("The number " + FloatValue +" squared has the value " +  Math.pow(FloatValue , 2) );
            break;  

        }

    System.out.print("\nDo you want to enter more data ? y/n: ");
        token = console.next();
        again = token.charAt(0);

        }
    while ( again == 'y');  
  }
}

4 个答案:

答案 0 :(得分:0)

你的流程错了。问题是,一旦你得到你的输入,你正在检查它是否小于0.如果是,你打印消息You must have a positive for square root.(即使用户真的想要执行square)而是只检查用户选择r选项。

像这样改变

do 
{

  System.out.print("Enter a float values: ");
  FloatValue = console.nextFloat();

  System.out.print("Do you want the (s)quare or square (r)oot of " + FloatValue +": ");
        SqrSqrt = console.next();


switch(SqrSqrt)
{
 case "r":   


   if (FloatValue < 0)
   {
       System.out.println("You must have a positive for square root.");
   } 
   else
      System.out.println("The square root of " + FloatValue +" is " +  Math.sqrt(FloatValue) );       
            break;


 case "s":    
            System.out.println("The number " + FloatValue +" squared has the value " +  Math.pow(FloatValue , 2) );
            break;  

}

    System.out.print("\nDo you want to enter more data ? y/n: ");
        token = console.next();
        again = token.charAt(0);

        }
    while ( again == 'y');  

答案 1 :(得分:0)

如上所述,我采用第二种方法来解决这个问题。

主要问题是你的if条件应该改为循环。

而不是:

 if (FloatValue < 0)
    {
    System.out.println("You must have a positive for square root.");
    System.out.print("Do you want the (s)quare or square (r)oot of " + FloatValue +": ");
    SqrSqrt = console.next();

    }

你可以写:

 while( ! ("s".equalsIgnoreCase(SqrSqrt)) && FloatValue < 0)
    {
    System.out.println("You must have a positive for square root.");
    System.out.print("Do you want the (s)quare or square (r)oot of " + FloatValue +": ");
    SqrSqrt = console.next();

    }

答案 2 :(得分:0)

除了明显的错误之外,我想提醒一下,Java没有内置的复数支持。你不能只Sqrt(-4)只返回NaN(首字母缩略词: N ot a N umber)。此外,不同的语言环境具有不同的编号格式(例如:美国使用“。”,FF使用“,”作为小数分隔符),默认情况下,Java使用您的系统区域设置。

如果您希望将结果作为2位小数位数作为使用美国语言环境的字符串视图:

public static String GetSqrt(double x) {
    return String.format(x < 0.0 ? "%.2fi" : "%.2f", Math.sqrt(-x), Locale.US);
}

假设您是初学者,这是一个演示:

import java.lang.Math;
import java.util.*;

public class HelloWorld{
    public static void main(String []args){
        double x=-4;
        System.out.println(String.format("sqrt(%.2f) is "+ GetSqrt(x), x));
    }
    public static String GetSqrt(double x) {
        return String.format(x < 0.0 ? "%.2fi" : "%.2f", Math.sqrt(-x), Locale.US);
    }
}

答案 3 :(得分:0)

嗯......如果你仍然没有找到你的解决方案或者如果你正在为这个问题寻找更有条理的方法,我认为你应该通过将问题分解成更小的问题并定义一个负责每个问题的函数来做到这一点。这些小问题。

现在使用这些较小的块来解决问题。

在这种情况下,您的问题可以分解为以下内容,

  1. 要求浮动输入。
  2. 要求操作:“r”或“s”
  3. 检查操作是否合适,如果没有则重新询问。
  4. 计算并打印结果。
  5. 要求用户选择继续,如果是,请转到第一步。
  6. 您可以采用结构化方式进行,如下所示,

        import java.util.Scanner;
    
        public class SquareOrRoot {
    
        static Scanner console = new Scanner( System.in );
    
        public static void main( String[] args ) {
            askFloatAndPerform();
            askAgain();
        }
    
        public static void askAgain() {
            System.out.print("\nDo you want to enter more data ? y/n: ");
            String token = console.next();
            char again = token.charAt(0);
            if ( again == 'y' ) {
                askFloatAndPerform();
                askAgain();
            }
        }
    
        public static void askFloatAndPerform() {
            System.out.print( "Enter a float values: " );
            float floatVal = console.nextFloat();
    
            // this will keep asking untill we have a proper oper
            String oper = checkOper( floatVal, "" );
    
            switch( oper ) {
                case "r":
                    System.out.println( "The square root of " + floatVal + " is " +  Math.sqrt( floatVal ) );
                case "s":
                    System.out.println( "The number " + floatVal + " squared has the value " +  Math.pow( floatVal , 2) );
            }
    
        }
    
        public static String checkOper( float floatVal, String oper ) {
            if( oper.equalsIgnoreCase( "r" ) && floatVal < 0 ) {
                System.out.println("You must have a positive for square root.");
                // ask for oper again
                oper = askOper( floatVal );
                // check it again
                oper = checkOper( floatVal, oper );
            }
            else if( oper.equalsIgnoreCase( "s" ) ){
                // Do nothing, oper is allright
            }
            // If anything other than "r" or "s"
            else {
                // ask for oper again
                oper = askOper( floatVal );
                // check it again
                oper = checkOper( floatVal, oper );
            }
            // By now the oper should be allright
            return oper;
        }
    
        public static String askOper( float floatVal ) {
            System.out.print( "Do you want the (s)quare or square (r)oot of " + floatVal +": " );
            String oper = console.next();
            return oper;
        }
    
    }
    

    我认为评论应该解释代码,但如果你不理解某些东西,请不要问。