我针对标题中的问题编写了代码,似乎遇到了一些问题。谁能给我一些关于我做错了什么的见解或提示。尤其是代码的“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) 我有一个布尔数组,稍后需要将它们显示给用户,而不是显示真或假,我想显示是或否。有人可以帮我吗?
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) 我正在尝试对代码进行微优化,我想知道将变量转换为布尔值哪个更快:
<?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) 我需要将字符串转换为bool值的帮助:
我一直在尝试从TopMost获取程序的值(真或假)并将其保存在我的设置中。
Settings1.Default["tm"] = ;
Settings1.Default.Save();
Run Code Online (Sandbox Code Playgroud)
我的设置'tm'的类型是布尔值(true,false),但是我只是在短时间内使用C#,所以我不确定如何保存TopMost是true还是false 。
在您说要在属性中使用一个之前,它是一个用户选项;我希望他们能够选择启用(true)还是关闭(false)的选项,但是将其保存并加载为bool值。
我如何向 Elvis Operator 申请以下内容:
Exp: b= a?.attr> 0
Run Code Online (Sandbox Code Playgroud)
结果应该是:如果a是null,b应该是null,否则,b应该是结果attr > 0。
有人可以告诉我这是怎么回事吗?
#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)
我试图弄清楚为什么我不能使用布尔方法进行按引用调用(是的,我也在擦除该方法后进行了测试),然后我意识到我的主方法中的布尔变量是自己改变的。请帮忙!
在数学中,我们有-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
我正在尝试编写一个名为的模板化函数unique(),该函数std::vector仅使用<vector>,<set>和<iostream>标头来检测 a 是否只有唯一元素。
template <typename T>
bool unique(const std::vector<T>& container){}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
既然我们都知道 null 被评估为 false,我怎么能在 Javascript 中写出“is null or true”呢?我试过了:
function isNullOrTrue(nullableBool){
return !(nullableBool != null && nullableBool != true);
}
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?
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).第二部分甚至不需要执行.如果它确实可以丢失很多性能
boolean ×10
c# ×3
c++ ×2
java ×2
javascript ×2
.net ×1
comparison ×1
hypotenuse ×1
netbeans ×1
null ×1
operators ×1
php ×1
python ×1
python-3.x ×1
stream ×1
swing ×1
templates ×1
variables ×1
vector ×1