dimanche 1 mai 2016

variable gets double value by applying an if/else statement

I have an issue with the following method. If I apply the if/else condition which is currently set as a comment /* */, the variable withdrawalAmountVar get double value (if balance is 100 and I withdraw 1 final balance is 98, if I withdraw 10 the final balance is 80). Why?

public void makeWithdrawalButton()
{
    //transfer user's input into withdrawalAmountVar
    withdrawalAmountVar = Integer.parseInt(text5.getText());

    //check if withdrawal should be allowed according to overdraft set
/*
if ( (moneyAmountVar -= withdrawalAmountVar) < overdraftVar)
{
    JOptionPane.showMessageDialog(null, "Withdrawal amount exceeds overdraft" );
    text5.setText("");
}

else
{
*/

    try
    {
        //make withdrawal from current deposit amount
        moneyAmountVar -= withdrawalAmountVar;

        output = new DataOutputStream(new FileOutputStream("datafile.dat") );
        output.writeInt(accountNumberVar);
        output.writeUTF(firstNameVar);
        output.writeUTF(lastNameVar);
        output.writeInt(moneyAmountVar);
        output.close();
    }
    catch(IOException e)
    {
        System.err.println( "Cannot make withdrawal");
        System.exit(1);
    }
}

Why won't swift recalculate the 'if' statement?

I'm trying to create a very simple 'guessing game' where the user has to guess how many fingers the computer has up (maximum 5 fingers).

Here's the thing. When the code executes and I press submit, even when the print logs registers a correct number, the app still prints the if statement for incorrect. Where am I going wrong?

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}




@IBOutlet weak var fingerInput: UITextField!

@IBOutlet weak var fingerResult: UILabel!

@IBAction func fingerSubmit(sender: AnyObject) {

    let realNumberofFingers = Int(arc4random_uniform(6))

    print(realNumberofFingers)

    if fingerInput != realNumberofFingers {

        fingerResult.text = "Gosh darn, that's wrong!"

    } else if fingerInput == realNumberofFingers {

        fingerResult.text = "Thats right!"

    }
}

}

GLSL skips "if" statement

So my GLSL fragment shader (don't think it matters) skips the "if" statement. The shader itself is very short (can't go further without solving this).

Basically I send some data via a uniform buffer object and use it further in the shader. However, the thing skips the assignment attached to the "if" statement for whatever reason.

I checked the values of the buffer object using glGetBufferSubData (tested with specific non zero values). Everything is where it needs to be. So I'm really kinda lost here. Must be some GLSL weirdness I'm not aware of.

Currently the

#version 420

layout(std140, binding = 2) uniform textureVarBuffer
{
    vec3    colorArray;             // 16 bytes
    int     textureEnable;          // 20 bytes
    int     normalMapEnable;        // 24 bytes
    int     reflectionMapEnable;    // 28 bytes

};

out vec4 frag_colour;

void main() {
    frag_colour = vec4(1.0, 1.0, 1.0, 0.5);

    if (textureEnable == 0) {
        frag_colour = vec4(colorArray, 0.5);    
    }
}

UNIX: cut inside if

I have a simple search script, where based on user's options it will search in certain column of a file.

The file looks similar to passwd

openvpn:x:990:986:OpenVPN:/etc/openvpn:/sbin/nologin
chrony:x:989:984::/var/lib/chrony:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin

now the function based on user's option will search in different columns of the file. For example

-1 "searchedword" -2 "secondword"

will search in the first column for "searchedword" and in the second column for "secondword" The function looks like this:

while [ $# -gt 0 ]; do
  case "$1" in
    -1|--one)
              c=1
              ;;
    -2|--two)
              c=2
              ;;
    -3|--three)
              c=3
          ;;
    ...
  esac

In the c variable is the number of the column where I want to search.

cat data | if [ "$( cut -f $c -d ':' )" == "$2" ]; then cut -d: -f 1-7 >> result; fi 

Now I have something like this, where I try to select the right column and compare it to the second option, which is in this case "searchedword" and then copy the whole column into the result file. But it doesn't work. It doesn't copy anything into the result file.

Does anyone know where is the problem? Thanks for answers

(At the end of the script I use:

shift
shift

to get the next two options)

Javascript IF statement not working consistently

I'm trying to create an if statement which triggers different functions for different levels of numerical form inputs. The code below ( http://ift.tt/1Ne8wr3 ) should should trigger GetTotalHigh() if the input > 100 and GetTotalLow() if the input <= 100 but doesn't seem to be working (i.e. it seems to triggers one of the functions at random). Any help much appreciated. Mike

<head>
  <script src="http://ift.tt/1ROJ5xx"></script>
  <script src="http://ift.tt/1RY4jZJ"></script>
  <script src="http://ift.tt/1XSXV5s"></script>
  <link href="http://ift.tt/1jAc5cP" rel="stylesheet" />
</head>

TOTAL:
<div id="chkTotal"></div>
<br>
<input type="text" name="input1" id="input1" />
<input type="button" value="Change X" onclick="Confirm(this)" />
<br>
<br>
<input type="text" name="input2" id="input2" />
<input type="button" value="Change Y" onclick="Confirm(this)" />

<script>

var input1 = 5555;
var input2 = 666;

$(document).ready(function() {
  document.getElementById("input1").value = input1;
  document.getElementById("input2").value = input2;
  GetFirstTotal();
});

function GetFirstTotal() {
  var total = 0;
  $('input[type=text]').each(function(index, value) {
    total += parseInt($(value).val() || 0);
  });

  $("#chkTotal").html(total);
}

function Confirm() {
  input = parseInt($('#input1').val() || 0, 10);
  input = parseInt($('#input2').val() || 0, 10);
  if (input <= 100) {
    GetTotalLow()
  };
  if (input > 100) {
    GetTotalHigh()
  };
}


function GetTotalHigh() {

  total = 0
  BootstrapDialog.confirm('High - are you sure you want to do this?', function(result) {
    if (result) {
      input1 = parseInt($('#input1').val() || 0, 10);
      input2 = parseInt($('#input2').val() || 0, 10);

      total = input1 + input2;

      $("#chkTotal").html(total);
    } else {
      document.getElementById("input1").value = input1;
      document.getElementById("input2").value = input2;
    }
  });
}

function GetTotalLow() {

  total = 0
  BootstrapDialog.confirm('Low - are you sure you want to do this?', function(result) {
    if (result) {
      input1 = parseInt($('#input1').val() || 0, 10);
      input2 = parseInt($('#input2').val() || 0, 10);

      total = input1 + input2;

      $("#chkTotal").html(total);
    } else {
      document.getElementById("input1").value = input1;
      document.getElementById("input2").value = input2;
    }
  });
}
</script>

list the leap years using for loop with if statment

i am looking for a way to find the list of leap years between the birthyear and today and print them

function listOfLeapYears(aYear) 
{
aYear = birthDate.getFullYear()
bYear = today.getFullYear()

document.write(aYear + '<br>')
document.write(bYear + '<br>')

    for (i = aYear; i <= bYear; )
    {
        if (isLeapYear(birthDate))
        {
        document.write(i + '<br>');
        }
    birthDate.setFullYear(i+1)

    } 

}


var birthYear, birthMonth, birthDay
    birthYear = parseFloat(window.prompt('please enter your birth year' + ''))
    birthMonth = parseFloat(window.prompt('please enter your birth month' + ''))
    birthDay = parseFloat(window.prompt('please enter your birth day' + ''))

birthDate = new Date(birthYear, birthMonth-1, birthDay)
today = new Date()

document.write('your birth date is ' + dateStringLong(birthDate) + '<br>')
document.write('you are ' + differenceInYears(today, birthDate) + ' years old' + '<br>' )

listOfLeapYears(birthDate)

the for loop with the if statement doesn't seems to work as expected what i want my program to do for example :

birthyear is 1991

today = 2016 is 1991 leap year => yes print it => no move to the next year is 2016 leap yes => yes print it => no stop ( bcuz it is equal to today year )

so any help would be nice

Powershell - Nested if Statement not working

I am trying to write the code for finding a leap year. However, there seems to be something wrong with the nested if and else format. Please suggest.

[int]$x = Read-Host -Prompt 'Input a year : ';

if ($x % 4 -eq 0) {
        Write-Host $x 'is a leap year';
    if ($x % 100 -eq 0){
        Write-Host $x 'is a leap year';
        if ($x % 400 -eq 0){
        Write-Host $x 'is a leap year';
        else {
            Write-Host $x 'is not a leap year';
            }
        }

    else {
            Write-Host $x 'is not a leap year';
        }
    }
else {
        Write-Host $x 'is not a leap year';
    }

}