簡體   English   中英

Java和C ++中的類似程序的不同輸出

[英]Similar program in java and c++ different output

我創建了一個模擬謎題的程序。 其中n個開關串聯連接(第一個開關的輸出連接到第二個開關的輸入)。 第一個開關始終具有輸入電源,並且最初所有開關都處於斷開狀態。 對於每次迭代,如果開關具有輸入功率,它將切換狀態。 如果開關具有輸入電源,並且其狀態為打開,則產生輸出。 末端有一個燈泡,我們必須找出它是否在給定的迭代次數下發光。 我這樣做是數學上的,這一次我試圖激發。

考慮2個開關的情況

power    Switch1  power   Switch2  power   Bulb
ON         OFF     OFF     OFF     OFF     OFF
ON         ON      ON      OFF     OFF     OFF
ON         OFF     OFF     ON      OFF     OFF
ON         ON      ON      ON      ON      ON 

注意:在第三次迭代中,由於有電源輸入,開關2的狀態已更改。

我的代碼在Java中給出了正確的輸出,但在C ++中卻給出了錯誤,這是C ++代碼

#include<iostream>

using namespace std;

int main(){
        int n=2;
        int taps=4;
        bool ans=false;
        bool state[n][taps];//={0};//=new boolean[n][taps];
        bool out[n][taps];//={0};//=new boolean[n][taps];
        bool power=true;
        for(int j=0;j<taps;j++){
            for(int i=0;i<n;i++){
                state[i][j]=false;
                out[i][j]=false;
            }
        }

        for(int j=0;j<taps;j++){
            for(int i=0;i<n-1;i++){
                if(j>0)
                    state[0][j] = !state[0][j-1];

                out[0][j]=(power && state[0][j]);

                if(j>0){    
                    if (out[i][j]==true){
                        if (out[i][j-1]==false)
                            state[i+1][j]=state[i+1][j-1];
                        else
                            state[i+1][j]= !state[i+1][j-1];
                    }else if(out[i][j]==false){
                        if(out[i][j-1]==true)
                            state[i+1][j]= !state[i+1][j-1];
                        else
                            state[i+1][j] = state[i+1][j-1];
                    }   
                }else
                    state[i+1][j]=false;
                out[i+1][j]= (state[i+1][j] && out[i][j]);
                ans=out[i+1][j];
            }
            cout<<ans<<endl;    
        }
        if(ans == true)
            cout<<"ON"<<endl;
        else
            cout<<"OFF"<<endl;

}

這是java代碼

public class turn {
    public static void main(String args[]){
        int n=2;
        int taps=4;
        boolean ans=false;
        boolean state[][]=new boolean[n][taps];
        boolean out[][]=new boolean[n][taps];
        boolean power=true;
        for(int j=0;j<taps;j++){
            for(int i=0;i<n-1;i++){
                if(j>0)
                    state[0][j] = !state[0][j-1];

                out[0][j]=power & state[0][j];

                if(j>0){    
                    if (out[i][j]==true){
                        if (out[i][j-1]==false)
                            state[i+1][j]=state[i+1][j-1];
                        else
                            state[i+1][j]= !state[i+1][j-1];
                    }else if(out[i][j]==false){
                        if(out[i][j-1]==true)
                            state[i+1][j]= !state[i+1][j-1];
                        else
                            state[i+1][j] = state[i+1][j-1];
                    }   
                }else
                    state[i+1][j]=false;
                out[i+1][j]=state[i+1][j] & out[i][j];
                ans=out[i+1][j];
            }
            System.out.println(ans);

        }
        if(ans == true)
            System.out.println("ON");
        else
            System.out.println("OFF");
    }
}

這可能是其中之一

out[0][j]=(power && state[0][j]);

&&是邏輯比較,因此如果powerstate[0][j]true ,則此代碼返回true 你可能想使用位運算符&這里

和這里:

out[i+1][j]= (state[i+1][j] && out[i][j]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM