小编sty*_*own的帖子

在Java中将数字提升到一个幂

这是我的代码.由于某种原因,我的BMI计算不正确.当我在计算器上检查输出时:(10/((10/100)^2)))我得到1000,但在我的程序中,我得到5.我不确定我做错了什么.这是我的代码:

import javax.swing.*;

public class BMI {
    public static void main(String args[]) {
        int height;
        int weight;
        String getweight;
        getweight = JOptionPane.showInputDialog(null, "Please enter your weight in Kilograms");
        String getheight;
        getheight = JOptionPane.showInputDialog(null, "Please enter your height in Centimeters");
        weight = Integer.parseInt(getweight);
        height = Integer.parseInt(getheight);
        double bmi;
        bmi = (weight/((height/100)^2));
        JOptionPane.showMessageDialog(null, "Your BMI is: " + bmi);
    }
}
Run Code Online (Sandbox Code Playgroud)

java

63
推荐指数
4
解决办法
35万
查看次数

Rails嵌套连接条件的Activerecord

我正在尝试用条件编写嵌套连接查询.

我现在的查询是:

Event.joins(:store => :retailer).where(store: {retailer: {id: 2}})
Run Code Online (Sandbox Code Playgroud)

哪个输出以下SQL:

   SELECT "events".* FROM "events" INNER JOIN "stores" ON "stores"."id" = "events"."store_id" INNER JOIN "retailers" ON "retailers"."id" = "stores"."retailer_id" WHERE "store"."retailer_id" = '---
:id: 2
'
Run Code Online (Sandbox Code Playgroud)

还有以下错误:

SQLite3::SQLException: no such column: store.retailer_id: SELECT "events".* FROM "events" INNER JOIN "stores" ON "stores"."id" = "events"."store_id" INNER JOIN "retailers" ON "retailers"."id" = "stores"."retailer_id" WHERE "store"."retailer_id" = '---
:id: 2
'
Run Code Online (Sandbox Code Playgroud)

它告诉我没有列store.retailer_id,但是,我可以运行以下查询,它将正常工作:

Event.first.store.retailer_id
  Event Load (0.2ms)  SELECT  "events".* FROM "events"   ORDER BY "events"."id" ASC LIMIT 1 …
Run Code Online (Sandbox Code Playgroud)

sql ruby-on-rails rails-activerecord

20
推荐指数
2
解决办法
2万
查看次数

当列类型为JSON时,Rails simple_form创建表单

我有一个FooBar带有三列的model():

Foo -> String
Bar -> JSON
Baz -> String
Run Code Online (Sandbox Code Playgroud)

我想为这个模型创建一个表单

Bar具有以下默认属性: {zing: {}, zaz: {}, laz: {}}

我想有以下输入:

f.input :foo
f.input :zing
f.input :zaz
f.input :laz
f.input :baz
Run Code Online (Sandbox Code Playgroud)

我尝试使用fields_for并传入每个键并将其转换为符号:

bar.each do |k,v|
f.input k.to_sym
end
Run Code Online (Sandbox Code Playgroud)

但我得到的错误是FooBar有未定义的方法:zaz

任何想法将不胜感激,谢谢.

ruby-on-rails simple-form simple-form-for

5
推荐指数
1
解决办法
2949
查看次数

给小数点时,Java程序崩溃,但使用int

当用户输入整数时,程序运行平稳,但当用户输入最后有小数的数字时,程序崩溃.

这些是我得到的错误:

    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:458)
    at java.lang.Integer.parseInt(Integer.java:499)
    at BMI.main(BMI.java:11)
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

import javax.swing.*;

public class BMI {
  public static void main(String args[]) {
    int height;  // declares the height variable
    int weight;  // declares the weight variable
    String getweight;
    getweight = JOptionPane.showInputDialog(null, "Please enter your weight in Kilograms");  // asks user for their weight
    String getheight;
    getheight = JOptionPane.showInputDialog(null, "Please enter your height in Centimeters");  // asks user for their height
    weight = Integer.parseInt(getweight);  // stores their weight
    height = …
Run Code Online (Sandbox Code Playgroud)

java

3
推荐指数
3
解决办法
1334
查看次数

Java Homework错误

我的简短代码中不断出现错误.我不确定有什么问题,能帮帮我吗?

码:

import java.util.Scanner;


public class Driver {
private int age, carValue, tickets;



public getInfo(){
    Scanner in = new Scanner(System.in);
    System.out.println("Enter Age: ");
    age = in.nextInt();
    System.out.println("Enter Car Value: ");
    carValue = in.nextInt();
    System.out.println("Enter the number of tickets you have: ");
    tickets = in.nextInt();
}





}
Run Code Online (Sandbox Code Playgroud)

这是我的主要内容:

class Main
{   public static void main(String args[])
{   
    Driver john;
    john = new Driver();
    john.getInfo();

}
}
Run Code Online (Sandbox Code Playgroud)

我的错误是:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method getInfo() is undefined for …
Run Code Online (Sandbox Code Playgroud)

java

0
推荐指数
1
解决办法
156
查看次数