Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我试图在JAVA程序中添加一个平衡系统,但失败了_Java_Android - Fatal编程技术网

我试图在JAVA程序中添加一个平衡系统,但失败了

我试图在JAVA程序中添加一个平衡系统,但失败了,java,android,Java,Android,作为一个新的程序员,我不知道方法是如何工作的。但当我使用对象时,它们能给我任何int、float或double值吗?它可以用作整数、浮点、双精度吗!?请帮帮我。谢谢 import java.util.Scanner; public class ProjectBookShop { static Scanner scan= new Scanner(System.in); static String[] books = {"Java", "C", "CSS"}; static final doub

作为一个新的程序员,我不知道方法是如何工作的。但当我使用
对象时,它们能给我任何int、float或double值吗?它可以用作整数、浮点、双精度吗!?请帮帮我。谢谢

import java.util.Scanner;

public class ProjectBookShop {
static Scanner scan= new Scanner(System.in);

static String[] books = {"Java", "C", "CSS"};
static final double studentDiscount = 0.3;
static final double teacherDiscount = 0.4;
static final double alienDiscount = 0.0;
static final int balance = 150;


public static void main(String[] args) {

    prln("<---- WELCOME TO OUR BOOK SHOP --->");
    prln("Today we are offering you" +balance + "Taka");
    prln("Which books do you want:\n Ans:");
    String usersChoice= scan.nextLine();

    if(books[0].toLowerCase().equals(usersChoice.toLowerCase()))
    {
        prln("You opted for Java.");
        calculatePrice(books[0]);

       //Problem starts from this line.
        if(balance => showPrice(calculatePrice(books[0])))
        {
            prln("You are eligible for buying this book!");
        }


    }else if(books[1].toLowerCase().equals(usersChoice.toLowerCase()))
    {
         prln("You opted for C.");
         calculatePrice(books[1]);
    }else if(books[2].toLowerCase().equals(usersChoice.toLowerCase()))
    {
        prln("You opted for Css.");
        calculatePrice(books[2]);
    }
    else
    {
        prln("We dont have this book.");
    }

}
// These are called methods!

static void calculatePrice(String bookName)
{
    double price = 200;
    prln("Are you a student, teacher or alien:\n Ans:");
    String answer = scan.nextLine();
    if(answer.toLowerCase().equals("student"))
    {
        price = price - (price*studentDiscount);
        showPrice(price);

    }else if(answer.toLowerCase().equals("teacher"))
    {
        price = price - (price * teacherDiscount);
        showPrice(price);
    }else if(answer.toLowerCase().equals("alien"))
    {
    price = price - (price * alienDiscount);
    showPrice(price);
    }else
    {
    prln("We dont offer any books for you!!");
    showPrice(price);
    }

}
static void showPrice(Object price)
{
prln("Your total price will be: "+ price);
prln("<---- Thanks for shoping with us. --->");
}

static void prln(Object anyObject)
{
    System.out.println(anyObject);
}

static void pr(Object anyObject)
{
    System.out.print(anyObject);
}
}
import java.util.Scanner;
公共类书店{
静态扫描仪扫描=新扫描仪(System.in);
静态字符串[]books={“Java”、“C”、“CSS”};
静态最终双学生折扣=0.3;
静态最终双教师折扣=0.4;
静态最终双alienDiscount=0.0;
静态最终整数余额=150;
公共静态void main(字符串[]args){
prln(“”);
prln(“今天我们向您提供”+余额+“塔卡”);
prln(“您想要哪本书:\n Ans:”);
字符串usersChoice=scan.nextLine();
if(books[0].toLowerCase().equals(usersChoice.toLowerCase())
{
prln(“你选择了Java。”);
计算价格(书籍[0]);
//问题从这一行开始。
如果(余额=>showPrice(计算价格(账簿[0]))
{
prln(“你有资格购买这本书!”);
}
}else if(books[1].toLowerCase().equals(usersChoice.toLowerCase()))
{
prln(“您选择C.”);
计算价格(书籍[1]);
}else if(books[2].toLowerCase().equals(usersChoice.toLowerCase())
{
prln(“你选择了Css。”);
计算价格(书籍[2]);
}
其他的
{
prln(“我们没有这本书”);
}
}
//这些叫做方法!
静态void calculatePrice(字符串bookName)
{
双倍价格=200;
prln(“您是学生、教师还是外国人:\n Ans:”);
字符串应答=scan.nextLine();
if(answer.toLowerCase().equals(“学生”))
{
价格=价格-(价格*学生折扣);
showPrice(价格);
}else if(answer.toLowerCase().equals(“教师”))
{
价格=价格-(价格*教师折扣);
showPrice(价格);
}else if(answer.toLowerCase().equals(“alien”))
{
价格=价格-(价格*alienDiscount);
showPrice(价格);
}否则
{
prln(“我们不为您提供任何书籍!!”;
showPrice(价格);
}
}
静态无效显示价格(对象价格)
{
prln(“您的总价为:“+价格”);
prln(“”);
}
静态无效prln(对象任意对象)
{
System.out.println(anyObject);
}
静态void pr(对象anyObject)
{
系统输出打印(任意对象);
}
}

是的,基本类型
int
double
boolean
可以分配给
对象
s。它是使用包装类型来完成的

  • 字节
    -
    字节
  • short
    -
    short
  • int
    -
    Integer
  • long
    -
    long
  • float
    -
    float
  • double
    -
    double
  • char
    -
    Character
  • boolean
    -
    boolean
  • void
    -
    void
    注意
    Void
    是特殊的。由于
    void
    没有值,因此可分配给
    void
    的唯一值是
    null
对象
s不能用作基本类型,除非您将其转换为正确的类型:

Object ob = myObjectStream.readObject();
Integer intg = (Integer) ob;
警告:试图将包装为
float
并指定为
Object
float
强制转换为
Integer
将导致引发
ClassCastException
。要防止这种情况,您可以使用
instanceof

Object ob = myObjectStream.readObject();
if(ob instanceof Integer) {
  int x = (Integer) ob;
  // Do something
} else if(ob instanceof Float) {
  float f = (Float) ob;
  // Do something else
} else {
  System.err.println("I don't support provided object class!!!");
}
您是否注意到
float
中的
float
赋值?这在两方面都起作用:

float x = 1.0f;
Float y = x;
float z = y; // == 1.0f
自动装箱/拆箱。如果您不想使用它,您仍然可以手工操作:

float x = 1.0f;
Float y = Float.valueOf(x);
float z = y.floatValue(); // == 1.0f
这适用于除
Void
之外的所有包装类型


此外,不是:

String s1, s2;
if(s1.toLowerCase().equals(s2.toLowerCase()) {
  // ...
}
使用: 字符串s1、s2; if(s1.等信号情况(s2){ // ... } 这将正确处理某些角大小写Unicode字符


操作员
=>
不存在。您的意思是
>=


如果方法
showPrice
总是被赋予
double
s,包装成
double
s,那么包装它们是没有意义的;包装需要时间。如果只赋予
double
s,则将其重写为以下内容:

static void showPrice(double price)
{
prln("Your total price will be: "+ price);
prln("<---- Thanks for shoping with us. --->");
}
静态无效显示价格(双倍价格)
{
prln(“您的总价为:“+价格”);
prln(“”);
}
这同样有效,而且速度更快


pr
从未被使用过!删除它并保存代码。另外,当类被加载时,它的所有方法都被加载。所以,如果删除它,还可以使程序启动更快


正如一些评论所建议的,您可以尝试比较
void
。再次重写
showPrice
: 静态双倍价格(双倍价格) { prln(“您的总价为:“+价格”); prln(“”); 退货价格; }
这使得比较工作正常。

您不想将Object用于方法参数。Object是Java中其他所有对象派生的基础对象,但在您的情况下,当参数(如double)传递给具有Object参数的方法时,接收方法不再“查看”它是一个双精度的,所以它不能解释为一个数字

您应该更改您的
prln
pr
方法,改为使用:

static void showPrice(double price)
{
    prln("Your total price will be: "+ price);
    prln("<---- Thanks for shoping with us. --->");
}

static void prln(String str)
{
    System.out.println(str);
}

static void pr(String str)
{
    System.out.print(str);
}
静态无效显示价格(双倍价格)
{
prln(“您的总价为:“+价格”);
prln(“”);
}
静态无效prln(字符串str)
{
系统输出打印项次(str);
}
静态无效pr(字符串str)
{
系统输出打印(str);
}

方法
showPrice
不返回任何值。因此,它不能用于比较。实际上,
calculatePrice
也不返回值,因此它不能用作
showPrice
的参数。
calculatePrice
调用
showPrice
,因此这可能也是一个问题<