我刚写了以下代码行:
if (++(data_ptr->count) > threshold) { /*...*/ } // example 1
Run Code Online (Sandbox Code Playgroud)
我的目的是在进行比较之前增加count数据结构中data_ptr指向的变量threshold,并且这行代码有效.
如果我data_ptr在进行比较之前想要增加,我会写这个:
if ((++data_ptr)->count > threshold) { /*...*/ } // example 2
Run Code Online (Sandbox Code Playgroud)
出于好奇,我也尝试了这行代码:
if (++data_ptr->count > threshold) { /*...*/ } // example 3
Run Code Online (Sandbox Code Playgroud)
并发现它的行为与第一个完全相同.
第一个问题: 为什么示例#3与示例#1的作用相同?这是运营商优先考虑的问题吗?标准中的东西?我不得不写一个快速的测试程序,因为答案对我来说并不明显.
第二个问题:我应该以if不同的方式撰写此声明 我可以先在自己的行上执行增量,然后测试条件以避免任何可能的混淆.这是必要的,或者前两个例子是否足够明显?
当我尝试在/减量中写一个后缀/前缀,然后在/减量中写一个post /前缀时,我得到以下错误:操作++/ - 的参数无效.
但是,据JLS称:
PostIncrementExpression:
PostfixExpression ++
Run Code Online (Sandbox Code Playgroud)
和
PostfixExpression:
Primary
ExpressionName
PostIncrementExpression
PostDecrementExpression
Run Code Online (Sandbox Code Playgroud)
所以写:
PostfixExpression ++ ++
Run Code Online (Sandbox Code Playgroud)
应该可能......有什么想法吗?
我在我的gcc上尝试过这个:
int a=1;
cout<<(--a)--;
Run Code Online (Sandbox Code Playgroud)
输出为0; 但改成它
cout<<--(a--);
Run Code Online (Sandbox Code Playgroud)
导致错误(减值操作数需要左值).有人可以告诉我这件事吗?
谢谢!
我最近开始学习Python,for循环的概念对我来说仍然有点混乱.我理解它通常遵循格式for x in y,其中y只是一些列表.
for-each循环for (int n: someArray)
成为for n in someArray,
并且for循环for (i = 0; i < 9; i-=2)可以表示为for i in range(0, 9, -2)
假设,而不是一个恒定的增量,我想i*=2,甚至i*=i.这可能,或者我必须使用while循环吗?
我有以下代码:JS Fiddle
<html>
<head>
<script>
function increase(){
var a = 1;
var textBox = document.getElementById("text");
textBox.value = a;
a++;
}
</script>
</head>
<body>
<button type="button" onclick="increase()">show</button>
<input type="text" id="text">
</body>
</html>?
Run Code Online (Sandbox Code Playgroud)
我想要做的是:
我哪里错了?
我有以下代码:
public class Book {
private static int sample1(int i) {
return i++;
}
private static int sample2(int j) {
return ++j;
}
public static void main(String[] arguments){
int i = 0;
int j = 0;
System.out.println(sample1(i++)); //0
System.out.println(sample1(++i)); //1
System.out.println(sample2(j++));//1
System.out.println(sample2(++j));//2
System.out.println(i);//2
System.out.println(j);//2
}
}
Run Code Online (Sandbox Code Playgroud)
我的预期输出在评论中.实际输出如下:
0
2
1
3
2
2
Run Code Online (Sandbox Code Playgroud)
我对函数调用和incemental运算符感到困惑.有人可以解释实际结果吗?
有没有办法使用n in增加属性值nth-child(n)来输出结果:
div:nth-child(1){
top: -5px;
}
div:nth-child(2){
top: -10px;
}
div:nth-child(3){
top: -15px;
}
div:nth-child(4){
top: -20px;
}
div:nth-child(5){
top: -25px;
}
Run Code Online (Sandbox Code Playgroud) 我对多线程增量的最佳性能进行了调查.我检查了基于同步,AtomicInteger和自定义实现的实现,如AtomicInteger,但使用parkNanos(1),对失败的CAS.
private int customAtomic() {
int ret;
for (;;) {
ret = intValue;
if (unsafe.compareAndSwapInt(this, offsetIntValue, ret, ++ret)) {
break;
}
LockSupport.parkNanos(1);
}
return ret;
}
Run Code Online (Sandbox Code Playgroud)
我基于JMH做了基准:明确执行每个方法,每个方法都消耗CPU(1,2,4,8,16次)并且只消耗CPU.每个基准测试方法在1-17线程上在Intel(R)Xeon(R)CPU E5-1680 v2 @ 3.00GHz,8 Core + 8 HT 64Gb RAM上执行.结果让我感到惊讶:
问题:
我尝试执行此测试几次,并且spike总是发生在不同的数字线程中.另外我在另一台机器上试过这个测试,结果是一样的.也许这是测试中的问题.在StackProfiler中自定义impl的"坏情况"中,我看到:
....[Thread state distributions]....................................................................
50.0% RUNNABLE
49.9% TIMED_WAITING
....[Thread state: RUNNABLE]........................................................................
43.3% 86.6% sun.misc.Unsafe.park
5.8% 11.6% com.jad.generated.IncrementBench_incrementCustomAtomicWithWork_jmhTest.incrementCustomAtomicWithWork_thrpt_jmhStub
0.8% 1.7% org.openjdk.jmh.infra.Blackhole.consumeCPU
0.1% 0.1% com.jad.IncrementBench$Worker.work
0.0% 0.0% java.lang.Thread.currentThread …Run Code Online (Sandbox Code Playgroud) 对此非常新,所以我希望我的标题中的术语正确.
我试图找出如何创建将执行以下操作的实例方法:
- 返回身份证号码.
- 由于每个对象是从类构造函数(实例化?)创建的,因此会为其分配唯一的整数ID号.第一个ID号是1,并且当实例化新对象时,将分配连续的数字.
我能够找到上面做的类/静态方法的例子但是我无法弄清楚如何用实例方法做到这一点.我的尝试如下:
class Coordinates
{
private int iD = 0;
private float xCoordinate;
private float yCoordinate;
public Coordinates()
{
//Asks for input and assigns it to the two variables below
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the X Coordinate followed by the return key");
xCoordinate = keyboard.nextDouble();
System.out.println("Please enter the Y Coordinate followed by the return key");
yCoordinate = keyboard.nextDouble();
iD++;
}
public getiD()
{
return iD;
}
}
Run Code Online (Sandbox Code Playgroud)
我的主要方法如下:
public class …Run Code Online (Sandbox Code Playgroud) package practicejava;
public class Query {
public static void main(String[] args) {
char ch = 66;
System.out.println("character= " + ch);
ch++;
System.out.println("character = " + ch);
}
}
Run Code Online (Sandbox Code Playgroud)
从技术上讲ch++;,它ch=ch+1;是相同的,但为什么我写作时会出现错误ch=ch+1;而不是ch++;?