小编Dam*_*son的帖子

为什么我的交换机块中的"变量可能没有被初始化"编译器错误?

使用switch block时遇到"变量可能未初始化"错误.

这是我的代码:

public static void foo(int month)
{
    String monthString;
    switch (month)
    {
        case 1: monthString = "January";
                break;
        case 2: monthString = "February";
                break;
    }
    System.out.println(monthString);
}
Run Code Online (Sandbox Code Playgroud)

错误:

Switch.java:17: error: variable monthString might not have been initialized
        System.out.println (monthString);
Run Code Online (Sandbox Code Playgroud)

据我所知,当您尝试访问尚未初始化的变量时会发生此错误,但是当我在交换机块中为其分配值时,我是否正在初始化它?

同样,即使月份是编译时常量,我仍然会收到相同的错误:

public static void foo()
{
    int month = 2;
    String monthString;
    switch (month)
    {
        case 1: monthString = "January";
                break;
        case 2: monthString = "February";
                break;
    }
    System.out.println(monthString);
}
Run Code Online (Sandbox Code Playgroud)

java variables switch-statement

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

子查询在 select 语句中返回多于 1 行

假设我有两个表,orderdetails 和 orders。

我想打印订单日期、订单状态和总价。

在 orderdetails 表中,有 orderNumber、quantityOrdered、priceEach 和 productCode。

在订单中,有状态和订单号。

我试过这段代码,但它不起作用。

SELECT orderDate, status,  (SELECT SUM(quantityOrdered * priceEach) AS prices FROM orderdetails GROUP BY orderNumber)

FROM orders o, orderdetails od

WHERE od.orderNumber = o.orderNumber
Run Code Online (Sandbox Code Playgroud)

mysql sql database

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

使用 User.create() 时,Rails 用户模型未分配 ID

我尝试寻找答案,但他们都使用 User.new 等而不是创建。

当在控制台中使用具有有效属性的 User.create() 时,我得到一个使用 nil 作为 ID 和 nil 作为时间戳创建的用户。

这是我的用户模型。

 class User < ApplicationRecord
  attr_writer :name, :email
  before_save {self.email = email.downcase}

  validates :username, presence:true,
                       length: {maximum: 30},
                       uniqueness:true


  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence:true,
                    length: {maximum:200},
                    format: {with: VALID_EMAIL_REGEX},
                    uniqueness: {case_sensitive: false}

  has_secure_password

  validates :password, presence: true,
                       length: {minimum:6}

end
Run Code Online (Sandbox Code Playgroud)

在我正在使用的 Rails 控制台中

User.create(username:"oiuedhioj", email:"damhan@dagr.com",password:"test",password_confirmation:"test")
Run Code Online (Sandbox Code Playgroud)

并回来

<User id: nil, username: "Damhan", email: nil, created_at: nil, updated_at: nil, password_digest: "$2a$10$oGtRgcHigaHh/UCVX4QdM.AOgyGur8Oud5MyKZheUcQ..."> 
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails

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

非常基本的python

得到错误,但我不知道为什么......(我正在学习)我的代码;

import random
input("Hit enter to roll the dice")
global answer
def rollDice():
    result = random.randrange(1,6)
    print ("It landed on.." + str(result))
    answer = input("would you like to play again? [y/n]")
rollDice();


if (answer == "y" or "Y"):
    rollDice();
Run Code Online (Sandbox Code Playgroud)

错误; (一些剧本的作品)

Hit enter to roll the dice
It landed on..5
would you like to play again? [y/n]y
Traceback (most recent call last):
  File "diceRoller.py", line 11, in <module>
    while (answer == "y" or "Y"):
NameError: name 'answer' is not defined
Run Code Online (Sandbox Code Playgroud)

python dice

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