logic

Java: Exception thrown in constructor, can my object still be created?

岁酱吖の 提交于 2019-11-29 04:13:17
Could you tell me can be some case when exception is throwing in constructor and object is not null. I mean some part of object is created and another is not.Like this public Test(){ name = "John"; // exception // init some other data. } I understand in this sitiation object Test will be null, but Can be situation that object test cannot be null (delete block of exception not answer :) ) ? A class instance creation expression always creates a new object if the evaluation of its qualifier and arguments complete normally, and if there is space enough to create the object. It doesn't matter if

Multi-variable switch statement in c#

ぃ、小莉子 提交于 2019-11-29 03:03:05
I would like use a switch statement which takes several variables and looks like this: switch (intVal1, strVal2, boolVal3) { case 1, "hello", false: break; case 2, "world", false: break; case 2, "hello", false: etc .... } Is there any way to do something like this in C#? (I do not want to use nested switch statements for obvious reasons). There is no built-in functionality to do this in C#, and I don't know about any library to do this. Here is an alternative approach, using Tuple and extension methods: using System; static class CompareTuple { public static bool Compare<T1, T2, T3>(this Tuple

Looking for advice on project. Parsing logical expression

倖福魔咒の 提交于 2019-11-29 02:38:16
I'm looking for some advice on my school project. I am supposed to create a program that takes a logical expression and outputs a truth table for it. The actually creating of the truth table for me is not difficult at all and I've already wrote the methods in Java for it. I would like to know if there are any classes in java that I could use to parse the expression for me and put it into a stack. If not I'm looking for help on parsing the expression. It's the parentheses that get me whenever I try and think it through. Also if this would be easier in any other language I would be open to doing

Mustache - How to detect array is not empty?

て烟熏妆下的殇ゞ 提交于 2019-11-29 02:10:16
问题 I want to implement the following logic with Mustache: {{#if users.length > 0}} <ul> {{#users}} <li>{{.}}</li> {{/users}} </ul> {{/if}} // eg. data = { users: ['Tom', 'Jerry'] } Should I modify the users structure to meet the need? For example: {{#hasUsers}} <ul> {{#users}} <li>{{.}}</li> {{/users}} </ul> {{/hasUsers}} // eg. data = { hasUsers: true, users: ['Tom', 'Jerry'] } 回答1: Sorry, this may be too late. But I had similar requirement and found a better way to do this: {{#users.length}}

Jasmine expect logic (expect A OR B)

爱⌒轻易说出口 提交于 2019-11-29 01:32:31
问题 I need to set the test to succeed if one of the two expectations is met: expect(mySpy.mostRecentCall.args[0]).toEqual(jasmine.any(Number)); expect(mySpy.mostRecentCall.args[0]).toEqual(false); I expected it to look like this: expect(mySpy.mostRecentCall.args[0]).toEqual(jasmine.any(Number)).or.toEqual(false); Is there anything I missed in the docs or do I have to write my own matcher? 回答1: Note: This solution contains syntax for versions prior to Jasmine v2.0. For more information on custom

ASP.Net: Conditional Logic in a ListView's ItemTemplate

China☆狼群 提交于 2019-11-29 01:07:21
I want to show certain parts of an ItemTemplate based according to whether a bound field is null. Take for example the following code: (Code such as LayoutTemplate have been removed for brevity) <asp:ListView ID="MusicList" runat="server"> <ItemTemplate> <tr> <% if (Eval("DownloadLink") != null) { %> <td> <a href="<%#Eval("DownloadLink") %>">Link</a> </td> <% } %> </tr> </ItemTemplate> </asp:ListView> The above gives the following run-time error: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control. So how can put some conditional logic

Recursive power function: Why does this work if there's no initial return value?

橙三吉。 提交于 2019-11-29 00:43:54
because power(base, exponent) has no return value unless exponent is 0, initially, shouldn't power(base, exponent -1) return 'undefined', and therefore be unmultipliable, initially? So, I am having trouble following the logic of this code. Why/how does it work? function power(base, exponent) { if (exponent == 0) return 1; else return base * power(base, exponent - 1); } It could be more concise: function power(base, exponent) { return exponent == 0? 1 : base * power(base, --exponent); } Howerver an iterative solution is very much faster: function powerNR(base, exp) { var result = 1; while(exp--

Subtraction operation using only increment, loop, assign, zero

孤街浪徒 提交于 2019-11-28 23:12:58
问题 I am trying to build up subtraction, addition, division, multiplication and other operations using only following ones: incr(x) - Once this function is called it will assign x + 1 to x assign(x, y) - This function will assign the value of y to x (x = y) zero(x) - This function will assign 0 to x (x = 0) loop X { } - operations written within brackets will be executed X times Using following rules it is straight forward to implement addition (add) like this: ADD (x, y) { loop X { y = incr (y)

ReferenceError: Invalid left-hand side in assignment

情到浓时终转凉″ 提交于 2019-11-28 21:14:39
my code for a rock paper scissors game (called toss) is as follows: var toss = function (one,two) { if(one = "rock" && two = "rock") { console.log("Tie! Try again!"); } // more similar conditions with `else if` }; When I enter in the parameters toss("rock","rock") I get this error code: "ReferenceError: Invalid left-hand side in assignment" How to fix it? What this error means and what other cases when this error can happen? You have to use == to compare (or even === , if you want to compare types). A single = is for assignment. if (one == 'rock' && two == 'rock') { console.log('Tie! Try again

Is ((a + (b & 255)) & 255) the same as ((a + b) & 255)?

北城余情 提交于 2019-11-28 17:41:32
I was browsing some C++ code, and found something like this: (a + (b & 255)) & 255 The double AND annoyed me, so I thought of: (a + b) & 255 ( a and b are 32-bit unsigned integers) I quickly wrote a test script (JS) to confirm my theory: for (var i = 0; i < 100; i++) { var a = Math.ceil(Math.random() * 0xFFFF), b = Math.ceil(Math.random() * 0xFFFF); var expr1 = (a + (b & 255)) & 255, expr2 = (a + b) & 255; if (expr1 != expr2) { console.log("Numbers " + a + " and " + b + " mismatch!"); break; } } While the script confirmed my hypothesis (both operations are equal), I still don't trust it,