标签: boolean

需要帮助:计算机科学 101 HW/JAVA

我针对标题中的问题编写了代码,似乎遇到了一些问题。谁能给我一些关于我做错了什么的见解或提示。尤其是代码的“if...else”部分。

这是问题#9。如果您单击该链接,它将显示问题的打印输出。 http://s21.postimg.org/nl2tmf5tj/Screen_Shot_2013_09_21_at_6_44_46_PM.png

这是我的代码:

import java.util.*;
public class Ch3Ex9 {
/**
 * This method asks user for an x,y coordinates of a point, then returns the
 * distance to the origin and which quadrant the point is in.
 */
public static void main(String[] args)
{
    double xCoord; //x Coordinant initialized to 3
    double yCoord; //y Coordinant initalized to 4
    double hypo; //hypotenuse

    //declare an instance of Scanner to read the datastream from the keyboard
    Scanner keyboard = new Scanner(System.in);

    //get …
Run Code Online (Sandbox Code Playgroud)

javascript java netbeans boolean hypotenuse

-2
推荐指数
1
解决办法
222
查看次数

java - 将布尔值更改为是 否

我有一个布尔数组,稍后需要将它们显示给用户,而不是显示真或假,我想显示是或否。有人可以帮我吗?

    public static boolean[] seen (boolean[] birds)
    {   String quit = "100";
        String ans = "";
        while(!ans.equals(quit))
        {   ans=JOptionPane.showInputDialog(null,"Which bird are you reporting? \n   1) Blue Tit\n   2) Blackbird\n   3) Robin\n   4) Wren\n   5) Greenfinch");
            if (ans.equals("1"))
            {   birds[0]=true;
            }
            else if (ans.equals("2"))
            {   birds[1]=true;
            }
            else if (ans.equals("3"))
            {   birds[2]=true;
            }
            else if (ans.equals("4"))
            {   birds[3]=true;
            }
            else if (ans.equals("5"))
            {   birds[4]=true;
            }
        }
        return birds;   
    }
public static void display(boolean[] newbirds)
    {   JOptionPane.showMessageDialog(null,"newbirds[0]+" "+newbirds[1]+" "+newbirds[2]+" "+newbirds[3]+" "+ newbirds[4]+""); …
Run Code Online (Sandbox Code Playgroud)

java swing boolean

-2
推荐指数
1
解决办法
5271
查看次数

PHP 中哪个更快:!!或(布尔)?

我正在尝试对代码进行微优化,我想知道将变量转换为布尔值哪个更快:

<?php
$a='test';
$result1 = !!$a;
$result2 = (bool)$a;
?>
Run Code Online (Sandbox Code Playgroud)

我不担心代码大小,只担心执行时间。

这里有一些基准,但它非常不确定(尝试了多次),所以我想知道 PHP 的源代码中会发生什么,看看它们是否有不同的处理方式:

<?php
$a = 'test';
for($c=0;$c<3;$c++){
    $start = microtime(true);
    for($i=0;$i<10000000;$i++){
        $result = !!$a;
    }
    $end =  microtime(true);
    $delta = $end-$start;
    echo '!!: '.$delta.'<br />';
}
$a = 'test';
for($c=0;$c<3;$c++){
    $start = microtime(true);
    for($i=0;$i<10000000;$i++){
        $result = (bool)$a;
    }
    $end =  microtime(true);
    $delta = $end-$start;
    echo '(bool): '.$delta.'<br />';
}
Run Code Online (Sandbox Code Playgroud)

结果

!!: 0.349671030045
!!: 0.362552021027
!!: 0.351779937744
(bool): 0.346690893173
(bool): 0.36114192009
(bool): 0.373970985413
Run Code Online (Sandbox Code Playgroud)

php boolean micro-optimization

-2
推荐指数
1
解决办法
228
查看次数

在C#中将字符串转换为布尔值

我需要将字符串转换为bool值的帮助:

我一直在尝试从TopMost获取程序的值(真或假)并将其保存在我的设置中。

Settings1.Default["tm"] = ;
Settings1.Default.Save();
Run Code Online (Sandbox Code Playgroud)

我的设置'tm'的类型是布尔值(true,false),但是我只是在短时间内使用C#,所以我不确定如何保存TopMost是true还是false 。

在您说要在属性中使用一个之前,它是一个用户选项;我希望他们能够选择启用(true)还是关闭(false)的选项,但是将其保存并加载为bool值。

c# boolean

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

C# Elvis 运算符可为空的布尔值

我如何向 Elvis Operator 申请以下内容:

Exp: b= a?.attr> 0
Run Code Online (Sandbox Code Playgroud)

结果应该是:如果anullb应该是null,否则,b应该是结果attr > 0

.net c# boolean operators

-2
推荐指数
1
解决办法
1675
查看次数

为什么布尔变量会更改(C ++)?

有人可以告诉我这是怎么回事吗?

#include <iostream>
using namespace std;
int main() {
    bool x = false;
    if (x = true) { cout << "x is true" << endl; }
    if (x = false) { cout << "x is false" << endl; } 
    // for some reason always prints "x is true".
}
Run Code Online (Sandbox Code Playgroud)

我试图弄清楚为什么我不能使用布尔方法进行按引用调用(是的,我也在擦除该方法后进行了测试),然后我意识到我的主方法中的布尔变量是自己改变的。请帮忙!

c++ variables program-entry-point boolean pass-by-reference

-2
推荐指数
1
解决办法
66
查看次数

如何获取 2 个 false 布尔值并返回 true 作为结果

在数学中,我们有-1 * -1 = 1. 当两个负数相乘时,我们得到一个正数。

但是在 C# 中bool,我无法找到一种方法来做到这一点。

int i = -1
int j = -1

bool bi = i>0
bool bj = j>0

Console.writeLine (bi) 
Console.writeLine (bj)
Console.writeLine (bi && bj)
Run Code Online (Sandbox Code Playgroud)

结果:

false

false

false
Run Code Online (Sandbox Code Playgroud)

显然, && 不是正确的运算符。哪个运算符允许我有 2 个布尔值,当两者都为 false 时,返回 true

即 Console.Writeline(false false) 结果 true

c# boolean

-2
推荐指数
1
解决办法
1160
查看次数

C++ 模板函数检查唯一向量

我正在尝试编写一个名为的模板化函数unique(),该函数std::vector仅使用<vector>,<set><iostream>标头来检测 a 是否只有唯一元素。

template <typename T>
bool unique(const std::vector<T>& container){}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

c++ templates boolean vector stream

-2
推荐指数
1
解决办法
91
查看次数

Javascript - 是 null 还是 true - 最优雅的解决方案是什么?

既然我们都知道 null 被评估为 false,我怎么能在 Javascript 中写出“is null or true”呢?我试过了:

function isNullOrTrue(nullableBool){
  return !(nullableBool != null && nullableBool != true);
}
Run Code Online (Sandbox Code Playgroud)

有一个更好的方法吗?

javascript null boolean

-2
推荐指数
1
解决办法
45
查看次数

Python 3测试顺序未定

string='a'
p=0
while (p <len(string)) & (string[p]!='c') :
                    p +=1

print ('the end but the  process  already died ')
       while (p <1) & (string[p]!='c') :
IndexError: string index out of range
Run Code Online (Sandbox Code Playgroud)

我想测试一个字符串结尾的条件(示例字符串长度= 1)为什么这两个部分都被执行了条件已经是假的!只要 p < len(string).第二部分甚至不需要执行.如果它确实可以丢失很多性能

python comparison boolean python-3.x

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