Java 负数还是十进制
嗨,伙计们,我只是想知道我的代码有什么问题,但我想我有一些逻辑错误 这将是该计划的最终结果Java 负数还是十进制,java,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 squa
"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)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:
像这样,这应该是正确的答案
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:
谢谢各位。我会等待你的回答和帮助
顺便说一句,这是代码^^
{
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');
}
}
你的流程是错误的。问题是一旦你得到你的输入,你就要检查它是否小于0。如果是,您正在打印消息,则平方根必须为正数。即使用户真的想执行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');
如上所述,我给出了解决这个问题的第二种方法 主要问题是您的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();
}
除了明显的错误之外,我想提醒大家,Java没有内置复数支持。您不能只返回Sqrt-4,它只返回NaN的首字母缩写:不是数字。此外,不同的地区有不同的编号格式,例如:美国使用。FF使用十进制分隔符,默认情况下Java使用您的系统语言环境 如果您希望使用US语言环境以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);
}
}
嗯。。。如果您仍然没有找到解决方案,或者您正在寻找一种更结构化的方法来解决此问题,我认为您应该将问题分解为更小的问题,并定义一个负责每个较小问题的函数 现在使用这些较小的块来解决问题 在这种情况下,您的问题可以分为以下几点: 请求浮点输入。 请求操作:r或s 检查操作是否合适,如果不合适,请重新询问。 计算并打印结果。 请用户选择继续,如果是,请转至步骤1。 您可以按如下所示的结构化方式进行操作
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;
}
}
我认为注释应该解释代码,但如果您不理解某些内容,请询问。您的系统区域设置是什么?它是否使用,作为十进制分隔符?您可以简单地将if FloatValue<0更改为whileFloatValue<0。@SarveshKumarSingh这是因为第一个if条件。那个应该是循环。@Prashant若我们换成循环,那个么若用户输入-4并想要执行平方运算呢。那么它就永远不会转到交换机。检查他也可以通过在检查时再设置一个条件来检查!s、 equalsIgnoreCaseSqrSqrt&&FloatValue<0@user7@Prashant是的,可以做到。你们的方法:若用户输入一个负数并选择r,你们循环直到他输入一个正数。我的代码认为它是无效的,然后再次返回供用户输入。先生,要求是当我输入一个负值(如-4.6)时输出,提示问我应该使用什么方法来平方-4.6还是平方根-4.6,如果我选择平方根,它应该说我应该有一个正值,它会再次要求我输入一个值方法,如果它是负数的平方或平方根。如果你选择平方,那么它将显示负值为平方的结果,先生,非常感谢。它起作用了。它确实有效。谢谢你帮助我,先生。我只希望有人能称呼我一次,先生,不要加上你在制造一个场景。谢谢你帮助我的人。并在那里分享想法。这对我有很大帮助,我在这里学到了一些新东西。虽然我只是一个初学者,但我很抱歉,因为这是我尊重一个人的方式。我无意冒犯任何人或制造事端。我只是用先生来表达对一个人的尊重,希望你能理解,这种情况还会发生。