简体   繁体   English

为什么在$符号之前打印负号?

[英]Why is the negative sign printing before the $ sign?

I am supposed to add a toString method to the banckAccount class. 我应该将一个toString方法添加到banckAccount类。

It should return a name separated by a comma and a space. 它应该返回一个用逗号和空格分隔的名称。 Ex: "Yana" and balance of 3.03 , the call yana.toString() should return the string "Yana, $3.03" . Ex: "Yana" ,余额为3.03 ,调用yana.toString()应该返回字符串"Yana, $3.03"

I tried to add: 我尝试添加:

public String toString() {
    return  name + ", " + "$"+ balance;
}

It works when I type in: 当我输入以下内容时,它可以工作:

"user, $90.01" 

But when I enter 但是当我进入

"Bankrupt Government, -$765432.10"

I keep getting: 我不断得到:

 "Bankrupt Government, $-765432.1"      

Code: 码:

import java.util.*;

public class BankAccount {

     String name;
     double balance;

     public void deposit (double amount ){
          balance = balance + amount;


     }

     public void withdraw ( double amount) {
          balance = balance - amount ;          
     }

  }//end of class

Your balance is negative so it prints as listed. 您的余额为负数,因此按列示打印。 It needs to be 它必须是

if(balance < 0){
   balance = balance * -1;
   return  name + ", " + "-$"+ balance;
}
else{
  return  name + ", " + "$"+ balance;
}

Your answer is right here: 您的答案就在这里:

return  name + ", " + "$"+ balance;

Java simply concatenates the string as you have defined it. Java只是将您定义的字符串串联起来。 So if balance is a negative number, you will get $ , followed by a negative number. 因此,如果balance为负数,您将获得$ ,后跟一个负数。

If you want it to display the - in the proper place, you can do something like this: 如果希望它在适当的位置显示- ,则可以执行以下操作:

String sign = (balance < 0) ? "-" : "";
System.out.println(name + ", " + sign + "$" + Math.abs(balance));

It is quite obvious, what your method toString() is doing. 很明显,您的toString()方法正在做什么。 If balance is negative, it is just added to the string with "-" sign after the "$". 如果余额为负,则将其仅添加到在“ $”之后带有“-”符号的字符串中。 I would detect, if the balance is positive or not: 我会检测出余额是否为正:

private String toString() { if (balance > 0.0) { return name + ", " + "$" + balance; } else { return name + ", -$" + (balance * (-1)); } }

Or 要么

private String toString() { return name + ", " + balance > 0.0 ? ("$" + balance) : ("-$" + (balance * (-1))); }

hope this helps run this program 希望这有助于运行该程序

      import java.util.*;
      import java.io.*;
      public class HelloWorld{
      public String toString() {
        if(balance<0)
        {
            balance= Math.abs(balance);
             return  name + ", " +"-"+ "$"+ balance;
        }else
        {
    return  name + ", " + "$"+ balance;
        }
}
       String name="sachin";
     double balance=-1000.00;
 public void deposit (double amount ){
          balance = balance + amount;


     }
      public void withdraw ( double amount) {
          balance = balance - amount ;          
     }
     public static void main(String []args){
       // System.out.println("Hello World");

            HelloWorld helloWorld = new HelloWorld();

         System.out.println(helloWorld.toString());

     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM